ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Why 48?
48 is the ascii code of '0' symbol.
If you want to replace text representation of digit (human readable) with its integer value (machine readable), you have to subtract the 'base' - ascii code of '0' char, since chars '0' - '9' follows each other
The following explains it:
Integer.parseInt(String str) and Integer.toString(int integer);
don't use things like int num = (ch-48) in any language, this was the way to do more than 30 years ago, but meanwhile there are library functions for each language to do this
48 is the ascii code of '0' symbol.
If you want to replace text representation of digit (human readable) with its integer value (machine readable), you have to subtract the 'base' - ascii code of '0' char, since chars '0' - '9' follows each other
The following explains it:
Thank you very much my friend,you realy help me
Quote:
a short google what atoi in java is brought
Integer.parseInt(String str) and Integer.toString(int integer);
don't use things like int num = (ch-48) in any language, this was the way to do more than 30 years ago, but meanwhile there are library functions for each language to do this
Thank you to my friend for the answer.I m new in Java and i dont know strings and any other library functions yet.I want to make a simple java calculator but i HAVE to use System.in.read,that s why i need to convert char to int,by the way i made it but the only problem is that i cant give characters more then 9(1-9) with system read.Any ideas?
Here it s my code
the only problem is that i cant give characters more then 9(1-9) with system read.Any ideas?
Not sure if the problem is that you can't "enter" things like A, B, C, D, E, and F to make hex numbers; or if you have problems with converting those numbers. On the conversion part, you can subtract '0', or in my viewpoint 0x30 and if the number is larger than 9, subtract 7.
ASCI A is 0x41
Subtract 0x30 gives you 0x11
0x11 is Decimal 17.
Subtracting 7 gives you 10, decimal. Which is what 0xA represents
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339
Rep:
Quote:
Originally Posted by NevemTeve
Actually, it is
Code:
num= ch-'0';
(I, for one, did work in EBCDIC)
That would be a little safer. still subtracting a fixed number to convert between "text" representation and numeric representation might not
be the most reliable. It is easy though. I am not sure if it will work with wide chars. Also, you need to make sure it is in range (i.e. '0'-'9') or you will get weird results.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.