Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Some Guy

Translating (or converting) an ASCII number character to an int

This topic is 5738 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I wrote a command-line program in Linux that would take a number as a parameter, divide it into 72, and then display the result. (Business majors would know the use of this as the Rule of 72.) But I''m stumped on the fact that I don''t know how to get an int from an ASCII number character. So let''s go into this, shall we? How would you do translate a number character into an int?

Share this post


Link to post
Share on other sites
Advertisement
I have two answers, one which is EXACTLY what you asked for, the other, a different way to accoplish what you want, but in a different way.

1)

to convert a string to a float try the function:

double atof( const char *string );

to convert a string to an int try the function:

int atoi( const char *string );

those are from MSDN btw...

2)

just get the actual number from the user!
you can use cin/cout if you want...:


float f = 0;
cout << "Float please: ";
cin >> f;


or it can be done with printf() and scanf().. don't remember those parameters though...

hope this helps!

tazzel3d ~ dwiel

[edited by - tazzel3d on December 4, 2002 9:00:28 PM]

Share this post


Link to post
Share on other sites
In C, an ASCII string is typically a char* (array of chars)
Since char is also a way to store a number from -128 to 127, you can use all the math plus/minus stuff the same way.

This is basically what the atoi function does:

Start with i=0, at the start of the string.
Each time you look at a character, multiply i by 10, subtract the decimal value for the number 0 from the current letter (this gives the binary number from 0 to 9), then add the result to i. Repeat until you find the end of the string.

Note: THERE IS NO ERROR DETECTION IN THE FOLLOWING CODE! the REAL atoi returns 0 if there is random garbage in your string.


  
int atoi(char *str)
{
int i=0;
for (; *str; ++str)
i = (10*i)+*str-'0';
return i;
}


Edit: I forgot the source tags!!!

[edited by - Nypyren on December 4, 2002 11:14:40 PM]

Share this post


Link to post
Share on other sites
So, assuming the user opens the program like this on a command line (where the name of the program is rule72):

rule72 8

I could just do this in the code:

cin >> argv;

Correct? If so, (BOOM!) that''s what I''ll do.

But... I don''t know. This just doesn''t sound right to me.


If you will, what I really want is some technical explanation on how it''s done. Let''s get detailed. Maybe that''ll solve some problems with my understanding.

Share this post


Link to post
Share on other sites
If you are using command line arguments, cin isn't what you want. Cin asks the user to input data after the program has been executed.

However, x = atoi(argv[1]) should do the trick (where x is your int). Note argv is an array of strings, one for each command line argument (argv[0] is used for the program name itself).

If you want to get technical, each ASCII character in C/C++ is stored as a number. When printing the number to the screen, printf or cout will translate the number in the appropriate character.

-Shameless lazy mistake edited out


[edited by - Psylent on December 5, 2002 4:11:34 AM]

Share this post


Link to post
Share on other sites
Lol... I haven't used command line parameters FOREVER...

Here's your entire program in strict old-school C.


    
#include <stdio.h>
#include <conio.h>

int main(int argc, char** argv)
{
int number;
float fraction;

if (argc != 1)
{
printf("Proper usage is... (whatever)\n");
return -1;
}

number = atoi(argv[1]);
fraction = (float)number/72.0f;
printf("Result: %f\n", fraction);
return 0;
}


Crud... did I forget the header for atoi?

[edited by - Nypyren on December 4, 2002 12:11:43 AM]

Share this post


Link to post
Share on other sites
quote:
Original post by Psylent
It just so happens the number used for the character "0" is 0 and the number used for the character "1" is 1.



What? Dude, you''re confused. The ASCII code for the character ''0'' is 48. Nypyren''s source works because he took ''0'' from the ASCII code, turning it into the actual digit the ASCII represents.

BTW, ''1'' == 49, ''2'' == 50, ..., ''9'' == 57

Peace,
Doc

Share this post


Link to post
Share on other sites
Err, yeah. Sorry ''bout that. Should''ve checked a copy of the ASCII table. That''s what you get for answering forum questions when you''re supposed to be working.



Share this post


Link to post
Share on other sites
Sorry bout the cin comment I ade earlier...

I mis read your question. Atoi is then what you want like everyone is saying.

my bad

tazzel3d ~` dwiel

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!