File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Convert character to Unicode" Watch "Convert character to Unicode" New topic

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.
Ritesh Agrawal
Ranch Hand

Joined: Jan 08, 2004
Posts: 74
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.
[ 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
I agree. Here's the link:
subject: Convert character to Unicode
It's not a secret anymore!