Convert a String of bytes into ASCII
It's unclear whether you're trying to actually decode a text string which you've got as a byte array, or whether you want a text representation (in hex) of arbitrary binary data. For the first you'd use:
String text = new String(data, 0, data.length, "ASCII");
For the second you could use something like Apache Commons Codec:
String text = Hex.encodeHexString(data);
Sonhja
Partially programmer, partially designer! An WPF lover, Android developer and .Net contributor! Learning more and more everyday :) And happy to help others work!
Updated on June 04, 2022Comments
-
Sonhja almost 2 years
I'm connecting to a bluetooth device that answers to some parameters I send it, but as a response I read from a socket like this:
String data = dIn.readLine();
Where dIn is a:
DataInputStream dIn = new DataInputStream(socket.getInputStream());
The thing is that I receive the data, but it's a byte array read on a string. How can I convert that string that contains my byte array into a String with the correct hexadecimal values?
Thank you in advance.
-
Nicolas over 12 years%X for uppercase alpha chars or %x for lowercase alpha chars.
-
Sonhja over 12 yearsMore or less this is what I needed... But not exactly.
-
Jon Skeet over 12 years@Sonhja: Ask a more specific question, get a more specific answer :)