• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

casting from char to byte not possible

 
Marius Constantin
Ranch Hand
Posts: 62
Java Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello experts,

Can you please please spear some time ? I have this peace of code and the compiler complains with an error that I can't get much sense of. I am attaching the printscreen to this post.


this is my environment:


Any thoughts ? And please be explicit. I am a Java noob..
java_err.PNG
error message printscreen
[Thumbnail for java_err.PNG]
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
char in java is of 2 bytes. But byte is 1 byte. So when you want to cast you would have to explicitly cast to byte, something like:

228 is the ASCII value for the '∑' symbol.

And when you are doing a narrowing cast- i.e cast from a data type of larger size to the data type of smaller size you would be losing the magnitude of the data which you would want to cast.

Please read this article for greater and clearer details
 
Campbell Ritchie
Sheriff
Posts: 48382
56
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Agree you will get greater detail from that link, but if it’s in the Java™ Language Specification, it will be by no means clearer
 
Marius Constantin
Ranch Hand
Posts: 62
Java Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mohamed Sanaulla wrote:char in java is of 2 bytes. But byte is 1 byte. So when you want to cast you would have to explicitly cast to byte, something like:

228 is the ASCII value for the '∑' symbol.

And when you are doing a narrowing cast- i.e cast from a data type of larger size to the data type of smaller size you would be losing the magnitude of the data which you would want to cast.

Please read this article for greater and clearer details


Thank you very much for all your help!

I want to do something like

given the character '∑' , assign the corresponding Unicode value to the variable b, and print the value of b on the screen. How can I do that ?

Thank you for all your time and understanding.
 
Marius Constantin
Ranch Hand
Posts: 62
Java Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Welcome to the Ranch

Agree you will get greater detail from that link, but if it’s in the Java™ Language Specification, it will be by no means clearer


thank you for your welcome Ritchie, Sheriff Sir. This ranch is famous all over the West ( Internet), I had to see it!

 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marius Constantin wrote:
I want to do something like

given the character '∑' , assign the corresponding Unicode value to the variable b, and print the value of b on the screen. How can I do that ?

Thank you for all your time and understanding.


I am thinking you could cast it to an int, but then you would get the value in Decimal number system. If you wanted in Hexadecimal then you can check out the java.lang.Integer API.
 
Marius Constantin
Ranch Hand
Posts: 62
Java Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mohamed Sanaulla wrote:
Marius Constantin wrote:
I want to do something like

given the character '∑' , assign the corresponding Unicode value to the variable b, and print the value of b on the screen. How can I do that ?

Thank you for all your time and understanding.


I am thinking you could cast it to an int, but then you would get the value in Decimal number system. If you wanted in Hexadecimal then you can check out the java.lang.Integer API.


I have tried int b = (int) '∑'; but still I get the same 6 errors. Any ides ?
 
Campbell Ritchie
Sheriff
Posts: 48382
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You cannot get 228 into a byte. And you need to provide some evidence that ∑ is in fact 228.
[campbell@??? ???]$ java SigmaDemo ∑→¥*Campbell
You don’t get 228 from that ∑. Look at the output, and you see what you get. You also see that the yen sign prints out at a5. Now if you print a5 in decimal, you see it comes out negative, as does the → arrow. You also see that the char turns into a byte simply by losing its left-hand (more significant) eight bits.>
 
Campbell Ritchie
Sheriff
Posts: 48382
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can divide the ∑ into two bytes with something like thisYou can find details of the SIZE variable here for example. Look at the constant field values link, too. There is probably some way of converting an int to a byte[] with a predefined method, but I can’t remember at present.
 
Campbell Ritchie
Sheriff
Posts: 48382
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you try (char)228, this is what you get: ä
 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Character class may also help you with this. If you do something like:

I am not so familiar with code points and what not, so I could be way off...

p.s. It looks from the error messages you have that you have other problems, not associated with the character conversion. I think your problem has to do with how your .java files are encoded. In order to get this to work I had to change the character encoding to either UTF-8 or UTF-16.
 
Campbell Ritchie
Sheriff
Posts: 48382
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That sounds a good idea, checking the encoding.
Encoding is not a “beginning” topic, so I shall move this discussion
 
Marius Constantin
Ranch Hand
Posts: 62
Java Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you for all your help ! I didn't have time to implemet your advices, so that's why it took so long to answer. shall get back to you as soon as possible.

thank you once again !
 
Campbell Ritchie
Sheriff
Posts: 48382
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You’re welcome
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic