aspose file tools*
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
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: 18570
    
    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: 19695
    
  20

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