LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-12-2014, 11:32 PM   #1
ianni
LQ Newbie
 
Registered: Nov 2014
Posts: 2

Rep: Reputation: Disabled
Convert char to int


Hello everybody,i have a question,i want to conver character to integer in Java,i find this and it s working but i cant understand why

I want to take the number 3 and not the ASCII value

char ch = '3';

int num = (ch-48);

why number 48?

Last edited by ianni; 11-12-2014 at 11:33 PM.
 
Old 11-13-2014, 12:47 AM   #2
Teufel
Member
 
Registered: Apr 2012
Distribution: Gentoo
Posts: 616

Rep: Reputation: 142Reputation: 142
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:
Code:
 Code    Char
  ..      ..
  ..      ..
  48      '0'
  49      '1'
  50      '2'
  51      '3'
  ..      ..
Here the ascii table:
http://www.cpptutor.com/ascii.htm

Last edited by Teufel; 11-13-2014 at 01:02 AM.
 
1 members found this post helpful.
Old 11-13-2014, 01:21 AM   #3
a4z
Senior Member
 
Registered: Feb 2009
Posts: 1,727

Rep: Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742
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
 
1 members found this post helpful.
Old 11-13-2014, 03:31 AM   #4
Teufel
Member
 
Registered: Apr 2012
Distribution: Gentoo
Posts: 616

Rep: Reputation: 142Reputation: 142
Use ch-48.
It's much less code.
it's much more faster.
It's much more clear.
It gives you an idea of how things work.
 
1 members found this post helpful.
Old 11-13-2014, 03:34 AM   #5
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Actually, it is
Code:
num= ch-'0';
(I, for one, did work in EBCDIC)
 
1 members found this post helpful.
Old 11-13-2014, 03:47 AM   #6
ianni
LQ Newbie
 
Registered: Nov 2014
Posts: 2

Original Poster
Rep: Reputation: Disabled
Quote:
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

Code:
      public static void main(String[] args)throws IOException
      {
              char ch = 0;
              float num1 = 0;
              float num2 = 0;
              float A[] = new float[3];
              
              for(int i=0; i<3; i++)
              {
                  A[i] = System.in.read();
              }
              
              
              for(int i=0; i<3; i++)
              {
                  if(A[i] >= 48 && A[i] <= 57)  
                  {
                      A[i] = A[i] - 48;
                      num1 = A[0];
                      ch = (char)A[1];
                      num2 = A[2];
                  } 
              }   
                      switch(ch)
                      {
                          case '+':
                              System.out.println(num1+num2);
                          break;  
                              
                          case '-':
                              System.out.println(num1-num2);
                          break;
                              
                          case '*':
                              System.out.println(num1*num2);
                          break;  
                               
                          case '/':
                              if(num2 == 0)
                              {
                                  System.out.println("cant divide with 0");
                              }
                              
                              else
                              {
                                  System.out.println(num1/num2);
                              }
                              
                          break;
                              
                          default:
                              System.out.println("bye bye");
                          break;  
                      }
                  }
              }
 
Old 11-13-2014, 12:56 PM   #7
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Quote:
Originally Posted by ianni View Post
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

The same works for A-F HEX digits.

Here's a very good ASCII table which I think has been online for like 10 or more years, I've used it for a very long period of time: http://web.cs.mun.ca/~michael/c/ascii-table.html

Last edited by rtmistler; 11-13-2014 at 12:57 PM.
 
1 members found this post helpful.
Old 11-13-2014, 01:03 PM   #8
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
Quote:
Originally Posted by NevemTeve View Post
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.
 
1 members found this post helpful.
Old 11-13-2014, 01:22 PM   #9
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Well, perhaps it is strtol (or a cousin of that) the OP is looking for. (Edit: if it were C-language, that is.)

Last edited by NevemTeve; 11-13-2014 at 01:23 PM.
 
1 members found this post helpful.
Old 11-13-2014, 01:46 PM   #10
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
Hmmm. It says "in Java"

Maybe Integer.valueOf is what you are looking for?
I don't know much about Java but being a very HLL I am sure there is a better way to do this.
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ Cannot convert from int to char or string. to_string not available. SparceMatrix Programming 9 08-03-2014 04:30 PM
How do I convert a char from a string to an int in C... trist007 Programming 4 08-25-2012 08:14 PM
Convert ip address from char* to unsigned int* Kunsheng Programming 6 04-29-2009 02:12 PM
convert char * to int? calorie712 Programming 6 05-01-2006 04:38 AM
convert unsigned char * to unsigned long int linux_lover2005 Programming 3 04-26-2005 11:38 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:16 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration