• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Short cast to char - bungle

 
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

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



Cheers,

Si.
 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Thanks for the response.

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

Cheers,

Si.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The char '8' is represented by the decimal value 56.

See the following ASCII table...
http://www.lookuptables.com/
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 485
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
can anyone explain the subtle diff between the char byte and short
 
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic