File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Can someone explain this ??? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Can someone explain this ???" Watch "Can someone explain this ???" New topic

Can someone explain this ???

Rob Hopping

Joined: Oct 29, 2004
Posts: 4
I've set convertionValue equal to (binaryString.charAt(0)) but when I print them out I get two different answers.

The following produces:


Why is it addind 48 to convertionValue?

// main line of program
public static void main (String[] args) {

String binaryString = ("3677895775421");
int convertionValue;

convertionValue = (binaryString.charAt(0));

Dun Dagda
Ranch Hand

Joined: Oct 12, 2004
Posts: 54
That is because charAt will return the ASCII code of the character at that position in the String. The ASCII code of the character 0 (zero) is 48, character 1 is ASCII 49, etc.
So to convert the character code for the character "1" to its numerical equivalent, you need to subtract 48 from the character code.


SCJP 1.4<br />SCWCD (in progress)
marc weber

Joined: Aug 31, 2004
Posts: 11343


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
Dun Dagda
Ranch Hand

Joined: Oct 12, 2004
Posts: 54
Here's something that might help you out.

Try pasting this into a .java file and compiling and running it yourself.

I agree. Here's the link:
subject: Can someone explain this ???
It's not a secret anymore!