• Post Reply Bookmark Topic Watch Topic
  • New Topic

Byte to character conversion  RSS feed

 
Sandeep Panda
Greenhorn
Posts: 8
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • 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.

 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • 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.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • 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?
 
Paul Clapham
Sheriff
Posts: 22835
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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 Java Tomcat Server
  • Mark post as helpful
  • send pies
  • 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
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!