This week's book giveaway is in the Big Data forum.
We're giving away four copies of Elasticsearch in Action and have Radu Gheorghe & Matthew Lee Hinman on-line!
See this thread 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 Elasticsearch in Action this week in the Big Data 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: 19351

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: 19908

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: 41084
There is a method in the java.lang.Character class called something like numericValue. That might help.
I agree. Here's the link:
subject: Convert from char to byte?