Odd Hex.encode behavior

Hi,

Here's an easy one (hope, hope)

I wanted to verify that my ascii to ebcdic conversion was going OK.

So I place a Hex.encode into my code to print the buffer contents like so:

byte[] buffer = cipher.doFinal(Hex.decode(hexIn.trim()));

asciiString = new String(buffer,charSetIn.trim());

System.out.println("ASCII string = " + asciiString);

System.out.println("ASCII Hex = " + new String(Hex.encode(buffer)));

and this is what I get:

ASCII string = XX2304429864723

ASCII Hex =

charSetIn is set to "windows-1252" (not important)

the doFinal doesn't throw an error (not important)

How can Hex.encode return a nonprintable String?<?!?!?!?

It seems that no matter what it's pointing at, it should "encode" it!

The .encode is from Apache, but I get the same results from BouncyC.

By the way, the println of asciiString looks fine.

Hope this is one of those, "Oh, the ol' xxxxx bug" things.

Thanks.

Scott

[1027 byte] By [jewellsca] at [2007-11-27 5:51:46]
# 1

Well I finally figured this one out.

The problem is that Hex.encode DOES NOT

return bytes in the local character set.

Hex.encode always returns bytes in ASCII

regardless of the character set being used.

Here's how the "encode" table is defined in the Encoder class:

protected final byte[] encodingTable =

{

(byte)'0', (byte)'1', (byte)'2', (byte)'3', (byte)'4', (byte)'5', (byte)'6', (byte)'7',

(byte)'8', (byte)'9', (byte)'a', (byte)'b', (byte)'c', (byte)'d', (byte)'e', (byte)'f'

};

Well, whatever characterset is "local" on the machine this

code is compiled on, determines the characters encode returns.

Hex.encode should probably build an encode table out of local

characters at runtime to avoid this problem. You can pretty much

assume that some poor IBM guy is going to take the output from

encode and do this with it:

str = new String(Hex.encode(byts))

This line of code fails on any machine with a characterset other than ASCII.

Java assumes the bytes are in the local characterset when it builds the string.

You can of course get around this problem by passing a characterset

into the String construction like so:

str = new String(Hex.encode(byts),"windows-1252")

Hope this helps.

Scott

jewellsca at 2007-7-12 15:41:29 > top of Java-index,Security,Cryptography...