Win a copy of Pipeline as Code this week in the Cloud/Virtualization forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

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: 10777
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: 12974
66
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: 26481
81
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: 12974
66
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.
 
That is a really big piece of pie for such a tiny ad:
SKIP - a book about connecting industrious people with elderly land owners
https://coderanch.com/t/skip-book
reply
    Bookmark Topic Watch Topic
  • New Topic