This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Convert character to Unicode Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Convert character to Unicode" Watch "Convert character to Unicode" New topic
Author

Convert character to Unicode

Angela lewis
Ranch Hand

Joined: Mar 01, 2004
Posts: 100
Page 76 Khalid Mughal
/***********/
char v1 = ')'; //Unicode value 41
byte v2 = 13;
int result1 = ~v1 ; //-42
/****************/
Plz tell me what is Unicode value and how did its complement get -42. I know how to convert Decimal value to Binary and vice-versa but I don't know how to convert Unicode to binary and vice versa.
Thanks
Ritesh Agrawal
Ranch Hand

Joined: Jan 08, 2004
Posts: 74
Hi,
Unicode is a character encoding format. For more info on this please refer Unicode. All Java files are considered as a raw stream of unicode characters. Please see JLS Sec 3.1. So, all the ASCII characters in Java are represented as Unicode escapes, whose values are represented in Hexadecimal format as '\uXXXX'. Here XXXX is the hexadecimal representation of the ASCII character. Lets take your example.
char v1 = ')';
ASCII Value for ')' = 41.
Corresponding Hexadecmial representation for 41 = 0x29.
Unicode representation = '\u0029';
So you could very well write as
char v1 = '\u0029';
When you do
int result = ~v1;
This is what happens.
Binary representation of v1
v1 = 0000 0000 0000 0000 0000 0000 0010 1001
~v1 = 1111 1111 1111 1111 1111 1111 1101 0110 (Invert the bits)
And ~v1 = -42
You can verify this result as follows:

Hope this helps.
cheers...
Ritesh
[ April 01, 2004: Message edited by: Ritesh Agrawal ]

Ritesh<br /> <br />SCJP 1.4<br />IBM Test 340<br />IBM AIX V4.0 Certified Professional<br /> <br />Right actions for the future are the best apologies for wrong ones in the past.<br />- Tyron Edwards
Angela lewis
Ranch Hand

Joined: Mar 01, 2004
Posts: 100
Thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Convert character to Unicode