Win a copy of Penetration Testing Basics this week in the Security forum!

# Short cast to char - bungle

Simon Cockayne
Ranch Hand
Posts: 214
Hi,

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

Cheers,

Si.

levani dvalishvili
Ranch Hand
Posts: 99
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
Hi,

Thanks for the response.

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

Cheers,

Si.

marc weber
Sheriff
Posts: 11343
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
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
hi
can anyone explain the subtle diff between the char byte and short

Joe Sondow
Ranch Hand
Posts: 195
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
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.