5

I am having a bytearray of byte[] type having the length 17 bytes, i want to convert this to string and want to give this string for another comparison but the output i am getting is not in the format to validate, i am using the below method to convert.I want to output as string which is easy to validate and give this same string for comparison.

    byte[] byteArray = new byte[] {0,127,-1,-2,-54,123,12,110,89,0,0,0,0,0,0,0,0};
    String value = new String(byteArray);
    System.out.println(value);

Output : ���{nY

game2011
  • 51
  • 1
  • 4
  • 1
    And what kind of output do you want to get? – Alex Abdugafarov Feb 16 '12 at 10:49
  • Try `new String(byteArray,"US-ASCII")` or new String(byteArray,"UTF-8")` or something similar. – Oliver Feb 16 '12 at 10:49
  • possible duplicate of [What is character encoding and why should I bother with it](http://stackoverflow.com/questions/10611455/what-is-character-encoding-and-why-should-i-bother-with-it) – Raedwald Apr 10 '15 at 12:04

5 Answers5

9

What encoding is it? You should define it explicitly:

new String(byteArray, Charset.forName("UTF-32"));  //or whichever you use

Otherwise the result is unpredictable (from String.String(byte[]) constructor JavaDoc):

Constructs a new String by decoding the specified array of bytes using the platform's default charset

BTW I have just tried it with UTF-8, UTF-16 and UTF-32 - all produce bogus results. The long series of 0 makes me believe that this isn't actually a text. Where do you get this data from?

UPDATE: I have tried it with all character sets available on my machine:

for (Map.Entry<String, Charset> entry : Charset.availableCharsets().entrySet())
{
    final String value = new String(byteArray, entry.getValue());
    System.out.println(entry.getKey() + ": " + value);
}

and no encoding produces anything close to human-readable text... Your input is not text.

Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
5

Use as follows:

byte[] byteArray = new byte[] {0,127,-1,-2,-54,123,12,110,89,0,0,0,0,0,0,0,0};
String value = Arrays.toString(byteArray);
System.out.println(value);

Your output will be

[0,127,-1,-2,-54,123,12,110,89,0,0,0,0,0,0,0,0]
Alex Abdugafarov
  • 6,112
  • 7
  • 35
  • 59
Chandra Sekhar
  • 18,914
  • 16
  • 84
  • 125
  • 2
    +1 because I believe this is what the OP actually wants. The numbers in the array clearly don't represent any kind of character encoding. – adelphus Feb 16 '12 at 11:01
4

Is it actually encoded text? If so, specify the encoding.

However, the data you've got doesn't look like it's actually meant to be text. It just looks like arbitrary binary data to me. If it isn't really text, I'd recommend converting it to hex or base64, depending on requirements. There's a good public domain base64 encoder you can use.

String text = Base64.encodeBytes(byteArray);

And decoding:

byte[] data = Base64.decode(text):
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Since it looks to me like he just wants to use string comparison, he could just encode the input string the same way and then compare both the encoded strings. – bart Feb 16 '12 at 12:45
1

not 100% sure if I get you right. Is this what you want?

String s = null;
StringBuffer buf = new StringBuffer("");
byte[] byteArray = new byte[] {0,127,-1,-2,-54,123,12,110,89,0,0,0,0,0,0,0,0};
for(byte b : byteArray) {
  s = String.valueOf(b);
  buf.append(s + ",");
}
String value = new String(buf);
System.out.println(value);
0

Maybe you should specify a charset:

String value = new String(byteArray, "UTF-8");
sinsedrix
  • 4,336
  • 4
  • 29
  • 53