• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Convert character to Unicode

 
Angela lewis
Ranch Hand
Posts: 100
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 74
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Angela lewis
Ranch Hand
Posts: 100
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic