aspose file tools*
The moose likes Java in General and the fly likes casting from char to byte not possible Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "casting from char to byte not possible" Watch "casting from char to byte not possible" New topic
Author

casting from char to byte not possible

Marius Constantin
Ranch Hand

Joined: Nov 23, 2011
Posts: 62

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..



[Thumbnail for java_err.PNG]

Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3097
    
  33

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


Mohamed Sanaulla | My Blog
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
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

Joined: Nov 23, 2011
Posts: 62

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

Joined: Nov 23, 2011
Posts: 62

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

Joined: Sep 08, 2007
Posts: 3097
    
  33

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

Joined: Nov 23, 2011
Posts: 62

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

Joined: Oct 13, 2005
Posts: 40052
    
  28
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

Joined: Oct 13, 2005
Posts: 40052
    
  28
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

Joined: Oct 13, 2005
Posts: 40052
    
  28
If you try (char)228, this is what you get: ä
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

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.


Steve
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
That sounds a good idea, checking the encoding.
Encoding is not a “beginning” topic, so I shall move this discussion
Marius Constantin
Ranch Hand

Joined: Nov 23, 2011
Posts: 62

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

Joined: Oct 13, 2005
Posts: 40052
    
  28
You’re welcome
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: casting from char to byte not possible