aspose file tools*
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
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Can someone explain this ???" Watch "Can someone explain this ???" New topic
Author

Can someone explain this ???

Rob Hopping
Greenhorn

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:

13
3
51

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));

System.out.println(binaryString.length());
System.out.println(binaryString.charAt(0));
System.out.println(convertionValue);
}
}
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.

DD


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

Joined: Aug 31, 2004
Posts: 11343

See http://www.asciitable.com/


"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
sscce.org
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.

DD
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can someone explain this ???