开发者

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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜