Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to convert String array to Byte array and then to string using following code.

It is working fine when byte is in between -128 to 127, but i am trying to convert larger than 127 when byte is in between 0 to 256 (like 133 or 155 etc.,) getting error like:

java.lang.NumberFormatException: Value out of range. value:"133"

String response = "[-47, 1, 16, 84, 2, 101, 110, 83, 111, 109, 101, 32, 78, 70, 67, 32, 68, 97]"; 
String[] byteValues = response.substring(1, response.length() - 1).split(",");
byte[] bytes = new byte[byteValues.length];

for (int i=0, len=bytes.length; i<len; i++) {
   bytes[i] = Byte.valueof(byteValues[i].trim());     
}

String str = new String(bytes);

Any help?!

share|improve this question

closed as off-topic by Boann, Sam, rene, TGMCians, gunr2171 Nov 6 '14 at 18:49

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – Boann, Sam, rene, TGMCians
If this question can be reworded to fit the rules in the help center, please edit the question.

    
You seem to want to treat the input as unsigned bytes. So what do you expect to do with the -47? –  Boann Nov 3 '14 at 16:47
    
Actually .doc, .docx files converted to byte array using .NET and return the response through WCF services. In android, application i am decoding that byte array to show that file. So, that byte array containing some values more than 127 like 133, 147, 196 etc., –  Vinay Krishna Nov 3 '14 at 17:42
    
How to retrieve string from that values –  Vinay Krishna Nov 3 '14 at 17:42
    
If the bytes encode a .doc file then why are you trying to construct a String from it? Also, you didn't answer my question: what do you expect to do with the -47? –  Boann Nov 3 '14 at 17:49
    
-47 which i mentioned in code is not related to my question and it is sample of converting byte array to string provided by someone please go through this link stackoverflow.com/a/6684822/4210834. –  Vinay Krishna Nov 3 '14 at 18:00

4 Answers 4

Per the java docs, a Byte is backed by a byte primitive which is implemented as an "8-bit signed two's complement integer" This has a max range of -128 to 127 and cannot handle any other values.

http://docs.oracle.com/javase/7/docs/api/java/lang/Byte.html

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

share|improve this answer
    
more importantly, unless you've set the charset to something that can actually read negative integers as valid characters; building a string out of signed integers like that won't result in a valid string. –  Malchia7 Nov 3 '14 at 16:56

If the value greater than 127, you need to store them in an int datatype. Now way of storing them in byte afaik.

share|improve this answer
    
If i store in an int datatype, i can't able to get exact string from that byte. –  Vinay Krishna Nov 3 '14 at 16:48
    
No other way? short, char, long? –  ControlAltDel Nov 3 '14 at 16:49
    
@ControlAltDel My bad. Wrongly worded and now corrected. –  sᴜʀᴇsʜ ᴀᴛᴛᴀ Nov 3 '14 at 16:50
    
@ControlAltDel I mean that using of bigger data type than byte. –  sᴜʀᴇsʜ ᴀᴛᴛᴀ Nov 3 '14 at 16:52

Byte can only have value up to 127 (2^7 - 1) (signed representation).

share|improve this answer

Java's "byte" type is unfortunately signed. To use it to store unsigned byte values (from 0 to 255), use an int and truncate it to a byte when storing:

bytes[i] = (byte)Integer.parseInt(byteValues[i].trim());

If you want to recover the original unsigned value, AND it with 0xFF (which casts it in an unsigned way to an int). E.g.:

for (byte b : bytes) {
    System.out.println(b & 0xFF);
}

The -47 in your input array makes no sense in this context, and will be interpreted as 256 - 47 = 209.


Edit: The constructor new String(bytes) is unsafe, because it uses an unspecified character set. Instead, specify a specific character set, whichever one you're using. E.g.:

String str = new String(bytes, java.nio.charset.StandardCharsets.UTF_8);
share|improve this answer
    
I have tried this, but my problem is i am getting the response from server in a byte array which contains 0 to 256, if i try to decode that byte array to string i am getting correct string when byte is in between 0 to 128 and getting ????? like that if byte is more than 127. How to decode that byte to correct string or is there any way instead of byte array. –  Vinay Krishna Nov 3 '14 at 17:26
    
@VinayKrishna You need to use the same character set for decoding the data as whatever character set the server uses for encoding the data. –  Boann Nov 3 '14 at 17:31

Not the answer you're looking for? Browse other questions tagged or ask your own question.