Reputation: 393
In my previous question, I pointed out the following problem:
byte[] a = new byte[] { 0x00, 0x00, 0x00, 0x25 };
String stringValue = new String(a, "273");
int firstResult = ByteBuffer.wrap(a).getInt();
System.out.println("First Integer Result: "+firstResult);
byte[] valueEncoded = Arrays.copyOfRange(stringValue.getBytes("273"), 0, 4);
System.out.println("Hex valueEncoded:");
for (byte b : valueEncoded){ System.out.format("0x%x ", b); }
int finalResult = ByteBuffer.wrap(valueEncoded).getInt();
System.out.print(System.lineSeparator());
System.out.println("Final Integer Result: "+finalResult);
This results in:
First Integer Result: 37
Hex valueEncoded:
0x0 0x0 0x0 0x15
Final Integer Result: 21
My expected result is 37, but the finalResult is 21. I got an answer with an explanation why this is the case:
bytes to string: EBCDIC 0x25 -> UTF-16 0x000A string to bytes: UTF-16 0x000A -> EBCDIC 0x15
0x000A is the standard line terminator on many systems, but it's generally output as "move to beginning of next line". Converting to IBM 273 is 'probably' because the text is destined for output on a device that uses that code page, and perhaps such devices want NL rather than LF for starting a new line.
My question is:
Can I somehow code a workaround in order to get the expected result while still using Strings (UTF-16) and decoding/encoding back and forth?
As mentioned in my previous post this is needed since we persist (Strings) to an DB2 table which then will be read by an IBM machine.
I was digging through several stackoverflow questions but could not find a solution to my problem:
Encoding strangeness with Cp500 (LF & NEL)
This answer is recommending the system property ibm.swapLF
but this not an option for me since I use OpenJDK 1.8.0_292-b10
and not any IBM JDK. Besides that my codepage is not supported by this property.
EDIT: I did some loggers, hope it helps.
Upvotes: 1
Views: 1060