pvllnspk
pvllnspk

Reputation: 5767

ASCII codes => string of characters

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?

Upvotes: 0

Views: 1409

Answers (1)

Marko Topolnik
Marko Topolnik

Reputation: 200148

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.

Upvotes: 3

Related Questions