This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes Convert from char to byte? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Convert from char to byte?" Watch "Convert from char to byte?" New topic

Convert from char to byte?

D diller

Joined: Nov 15, 2007
Posts: 29
I am having a heck of a time converting from a char to a byte - probably because chars are two bytes...

In either case, whenever I try to convert I get the ASCII value of the character.

if aChar = 4, I get 52 for the value of digit, which is the ASCII value of 4.

Any clues?
Paul Clapham

Joined: Oct 14, 2005
Posts: 18541

I suppose you mean that aChar = '4'. Then this is normal, the character is cast to its Unicode representation. Did you want something else to happen? If so, then what?
D diller

Joined: Nov 15, 2007
Posts: 29
Wanted digits = 4 like it would if I converted from a String.
Rob Spoor

Joined: Oct 27, 2005
Posts: 19675

Use '4' - '0'. Be careful though to handle the case where your character is not between '0' and '9' inclusive.

How To Ask Questions How To Answer Questions
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 38467
There is a method in the java.lang.Character class called something like numericValue. That might help.
It is sorta covered in the JavaRanch Style Guide.
subject: Convert from char to byte?