• 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

Byte to character conversion

 
Greenhorn
Posts: 8
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,


Please have a look at the code:



It says possible loss of precision. My doubt is, char is of 2 bytes whereas byte contains 1 byte. So, assigning byte to char should not be a problem. What am I missing?


Any help will be highly appreciated.

Thanks in advance.

 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

That means that byte(-128) will be translated as char(255).


Complete nonsense. It won't do the conversion; and I get the message "Type mismatch: cannot convert from byte to char".

Listen to Fred.

Winston

My apologies. I edited my (incorrect) reply instead of replying to it. Not my day.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What are the possible values for a byte? and a char?

How are you going to store (approx) half of those values?
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

fred rosenberger wrote:What are the possible values for a byte? and a char?

How are you going to store (approx) half of those values?



It's not "half" of the possible char values which can't fit into a byte. It's 255/256 of them.
 
Sandeep Panda
Greenhorn
Posts: 8
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes.. I am able to understand now..The range of char is 0 to 65535 (as char is unsigned) whereas it is -128 to 127 for byte..so, from -128 to -1,the numbers don't fit into a char.
 
fred rosenberger
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:

fred rosenberger wrote:What are the possible values for a byte? and a char?

How are you going to store (approx) half of those values?



It's not "half" of the possible char values which can't fit into a byte. It's 255/256 of them.


it wouldn't be to hard to agree on a byte value of 1 - 127 to a similar valued char, but mapping -128 through -1 would not be as obvious what the value should be.
 
reply
    Bookmark Topic Watch Topic
  • New Topic