Unable to convert bytes from charset 65535 in to Japanese (5035)

I have a character set conversion issue:

I am updating Japanese Kanji characters in DB2 in iSeries system with the following conversion method:

AS400 sys = new AS400("<host>","username","password");
CharConverter charConv = new CharConverter(5035, sys);
byte[] b = charConv.stringToByteArray(5035, sys, "試験");
AS400Text textConverter = new AS400Text(b.length, 65535,sys);

While retrieving, I use the following code to convert & display:

CharConverter charConv = new CharConverter(5035, sys);
byte[] bytes = charConv.stringToByteArray(5035, sys, dbRemarks);
String s = new String(bytes);
System.out.println("Remarks after conversion to AS400Text :"+s);

But, the system is displaying garbled characters while displaying. Can anybody help me to decode Japanese characters from binary storage?

Jon Skeet
people
quotationmark

Well I don't know anything about CharConverter or AS400Text, but code like this is almost always a mistake:

String s = new String(bytes);

That uses the platform default encoding to convert the binary data to text.

Usually storage and retrieval should go through opposite processes - so while you've started with a string and then converted it to bytes, and converted that to an AS400Text object when storing it, I'd expect you to start with an AS400Text object, convert that to a byte array, and then convert that to a String using CharConverter when fetching. The fact that you're calling stringToByteArray in both cases suggests there's something amiss.

(It would also help if you'd tell us what dbRemarks is, and how you've fetched it.)

I do note that having checked some documentation for AS400Text, I've seen this:

Due to recent changes in the behavior of the character conversion routines, this system object is no longer necessary, except when the AS400Text object is to be passed as a parameter on a Toolbox Proxy connection.

There's similar documentation for CharConverter. Are you sure you actually need to go through this at all? Have you tried just storing the string directly and retrieving it directly, without going through intermediate steps?

people

See more on this question at Stackoverflow