This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund 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
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Convert from char to byte?" Watch "Convert from char to byte?" New topic
Author

Convert from char to byte?

D diller
Greenhorn

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.

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

Any clues?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 16483
    
    2

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
Greenhorn

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

Joined: Oct 27, 2005
Posts: 19216

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


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32654
    
    4
There is a method in the java.lang.Character class called something like numericValue. That might help.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Convert from char to byte?
 
Similar Threads
How can I do this?
ASCII TO EBCDIC conversion preserving COMP-3
How to print ASCII values of all characters?
convert character to ascii and back
Packed decimal conversion - Java