• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Short cast to char - bungle

 
Simon Cockayne
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Why does my_byte (value 56) when cast to a char, givea value of 8?



Cheers,

Si.
 
levani dvalishvili
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
decimal equvalent of char 8 is 56
since you are asigning a byte to a char it assumes a decimal char value and gives you number 8 .
[ April 10, 2005: Message edited by: levani dvalishvili ]
 
Simon Cockayne
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Thanks for the response.

Why is "decimal equvalent of char 8 is 56"?

Cheers,

Si.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The char '8' is represented by the decimal value 56.

See the following ASCII table...
http://www.lookuptables.com/
 
Saurabh Khanna
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
char is UNSIGNED 16 bit integral type where as byte is signed. The main point is that the 16 bits in char are used to represent characters according to UNICODE character set.

Saurabh
 
Parameswaran Thangavel
Ranch Hand
Posts: 485
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
can anyone explain the subtle diff between the char byte and short
 
Joe Sondow
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
byte: 8-bit signed integer. Range -128 to 127.
short: 16-bit signed integer. Range -32768 to 32767.
char: 16-bit unsigned integer. Range 0 to 65535.
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another way of answering is that a char is a 16 bit unsigned number, while a short is a 15 bit number plus a one bit sign on the left. You may want to read up on two's complement binary notation, it is part of the SCJP exam syllabus.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic