aspose file tools
The moose likes Java in General and the fly likes Characters in String : Unicode 16-bit to custom 32-bit Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Characters in String : Unicode 16-bit to custom 32-bit" Watch "Characters in String : Unicode 16-bit to custom 32-bit" New topic
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
    
    3

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
    
    3

 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Characters in String : Unicode 16-bit to custom 32-bit
 
Similar Threads
Java language and Unicode
Totally Hexed...
converting C++ type struct to Java class?
Int size and double size
Print/display the raw bits (the 1s and 0s)