I call a library method that returns an object of type Serializable. In most cases the value is a simple String, so I typecast the returned value to String. I do the following to retrieve the String:
String val = (String)data.get("MyString");
There is a problem though when the String retrieved contains non-ascii. For example 'Køllert', the value that is returned is displayed as 'KxF8llert' The 'ø' is replaced with xF8 which is the corresponding Unicode Hex value.
When I print out the value as bytes, the character prints as -8.
byte[] defaultBytes = val.getBytes();
for(int ii=0; ii<defaultBytes.length; ii++) print((int)defaultBytes[ii]);
Is there a way to 'clean' the returned string to be printable as standard Unicode so the character is correctly displayed.
Edit
When I input the actual string as follows, the string can be correctly printed and when the bytes are examined, the character takes up two bytes with the integer values -61 and -72. Maybe it is returning UTF-8 instead of Unicode?
String val1 = "Køllert";
byte[] defaultBytes1 = val1.getBytes();
for(int ii=0; ii<defaultBytes1.length; ii++) print((int)defaultBytes1[ii]);
Solution
Sorry that the question may have been vague. The following seems to work for me. It's not so complicated, but had me spinning.
String val = new String(data.get("MyString").getBytes("UTF-8"));