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.
精彩评论