| Author |
Characters in String : Unicode 16-bit to custom 32-bit
|
Timothy Toe
Ranch Hand
Joined: Oct 19, 2002
Posts: 156
|
|
I understand that internally in Java, characters in Strings are actually Unicode characters, with each character represented with 16 bits. So, character �L� in Unicode is 0x004C which is also 0000 0000 0100 1100 Now, I wish to encode each of the 4 bits above into individual ASCII characters: = 0 0 4 C = 0x30 0x30 0x34 0x43 = 00110000 00110000 00110100 01000011 So, from the original 16-bit character in Java, I want a final 32-bit. Eventually, I�ll need to send the final result over the network, via OutputStream/writer and socket. Can someone help me on this ? Or give me some ideas... Thanks.
|
 |
Jesper de Jong
Java Cowboy
Bartender
Joined: Aug 16, 2005
Posts: 12929
|
|
Just cast the char to an integer and print out the value of the integer in hexadecimal notation. (This doesn't print the leading zeroes, by the way...).
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
Timothy Toe
Ranch Hand
Joined: Oct 19, 2002
Posts: 156
|
|
Your suggested code will produce "4C" which is not the final result that I require. I require each half byte - "4" and "C" - be encoded into their ASCII hex representation ..which is : 0x34 and 0x43. Thanks.
|
 |
Jesper de Jong
Java Cowboy
Bartender
Joined: Aug 16, 2005
Posts: 12929
|
|
|
|
 |
 |
|
|
subject: Characters in String : Unicode 16-bit to custom 32-bit
|
|
|