This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan 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 Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
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: 18541
    
    8

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: 19655
    
  18

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 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38062
    
  22
There is a method in the java.lang.Character class called something like numericValue. That might help.
 
jQuery in Action, 2nd edition
 
subject: Convert from char to byte?
 
Similar Threads
Packed decimal conversion - Java
How to print ASCII values of all characters?
ASCII TO EBCDIC conversion preserving COMP-3
How can I do this?
convert character to ascii and back