简体   繁体   中英

Convert hex to special characters (Java)

I need to send the length of the message in the first two bytes.

Calculated the message length and it came out to be 752 lets say ie 02F0 in hex. Now I need to figure the special symbol corresponding to this hex value. For getting those I use

 int num1 = Integer.parseInt("F0", 16);
 char c1 = (char) num1;

to get -> ð The first character is not visible there (due to the encoding), but I do get the right ones. Anyhow but when I concatenate those special characters to the final message string (in ASCII) then it changes from 02F0 to 023f (confirmed that by reading the final message in ultraedit hex view). Why is that happening? 3F is 63 in decimal and I see the same thing happening with the following code snippet as well->

Charset asciicharset = Charset.forName("ASCII");
Charset iso88591charset = Charset.forName("ISO-8859-1");

byte [] a = new byte[]{(byte)0x02, (byte)0xF0};

ByteBuffer inputBuffer = ByteBuffer.wrap(a);

CharBuffer data = asciicharset .decode(inputBuffer);

ByteBuffer outputBuffer = asciicharset .encode(data); --> This is where instead of 240
for F0 I get 63, in fact I tried with any value more than 63 but it always comes back to
that

byte[] outputData = outputBuffer.array();

Any help would be appreciated. Thanks.

I suspect your problem is not happening on the line you've commented but the previous line, that is:

 CharBuffer data = asciicharset .decode(inputBuffer);

ASCII characters are 7-bit, not 8-bit. So any value greater that 127 (0x7F in hex) is not going to be in your asciicharset, so the decode() is probably using a default replacement. (Note that 63 corresponds with the character '?', which makes sense as a default replacement.)

UPDATE: From the comment threads above and below, now that I understand a bit more about what you're trying to do, I would suggest not using a Charset at all (as none of the standard named encodings there will work for any/all combinations of 2 bytes you might have for the length, even UTF-16 has reserved values for example). Instead, I would suggest just using a byte buffer and only trying to convert to a string after you have stripped the initial length bytes from this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM