0

I have the next string of the ASCII codes:

[-76,-96,-80,106,-58,106,-1,34,7,123,-84,101,51]

What is the best way to convert it in the string of the characters of these codes values? Are there any pitfalls here?

Anirudh Ramanathan
  • 45,145
  • 22
  • 127
  • 184
pvllnspk
  • 5,471
  • 11
  • 56
  • 94
  • 1
    Those aren't all ASCII - you've got values which are negative Java byte values, so logically unsigned values over 127. ASCII is a 7-bit encoding. – Jon Skeet Jan 21 '13 at 11:53
  • These values are not part of the standard ascii table, so depending on the version of the table you are using they will be different symbol – Ivaylo Strandjev Jan 21 '13 at 11:53

1 Answers1

3

You need to turn it into a corresponding byte array and then instantiate new String(byteArray).

String [] strings = input.substring(1, input.length()-1).split(",");
byte[] bytes = new byte[strings.length];
int i = 0;
for (String s : strings) bytes[i++] = Byte.parseByte(s);
System.out.println(new String(bytes, "UTF-8"));

In place of "UTF-8" use your proper character encoding. It could be CP-1250, ISO-8859-1, or similar.

Marko Topolnik
  • 188,298
  • 27
  • 302
  • 416
  • only: String [] strings = input.substring(1, input.length()-2).split(","); – pvllnspk Jan 21 '13 at 12:19
  • You may have some extra whitespace at the end, `length()-1` is appropriate for the string you have posted. Use `input.trim().substring(...` to make sure you eliminate whitespace. – Marko Topolnik Jan 21 '13 at 13:34