开发者

How do I parse a byte array removing all non-ASCII bytes?

开发者 https://www.devze.com 2023-03-28 08:59 出处:网络
I\'m trying to log a bluetooth connection in an Android app (non-malicious, it\'s logging diagnostics).However, the transmissions and responses are in byte arrays and contai开发者_JAVA技巧n non-ASCII

I'm trying to log a bluetooth connection in an Android app (non-malicious, it's logging diagnostics). However, the transmissions and responses are in byte arrays and contai开发者_JAVA技巧n non-ASCII bytes. Trying to log the raw byte array gives about 20 spaces between every other character.

How can I remove all non-ASCII bytes?


Iterate through the byte array and append each byte that is in the ASCII range to a StringBuilder. Remember to cast them to char such that you get append(char) rather than append(int).


Since you are bothering to log the data at all perhaps you should log the hex-encoding of the data. That way you'll preserve all the information present. If you really want to see only the ASCII characters you can post-process the log information to give you an ASCII view of it when you want.

0

精彩评论

暂无评论...
验证码 换一张
取 消