pls help me to convert char to integer.

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Sep 2005
Posts: 17
Reputation: perlsu is an unknown quantity at this point 
Solved Threads: 0
perlsu perlsu is offline Offline
Newbie Poster

pls help me to convert char to integer.

 
0
  #1
Feb 16th, 2006
I have a problem in converting char to integer. My code is as follow:

char lang [2][4] ={"en","fr"};

char ch = lang[0][0];
int i = sprintf("%d",ch); //want to get integer value of 'e' in string "en"

ch = lang[0][1];
int j = sprintf("%d",ch);

The error is "sprintf cannot accept parameter 2 from const char*to char".

pls help me.
Thanks.

Rgds,
su
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

Re: pls help me to convert char to integer.

 
0
  #2
Feb 16th, 2006
I think it's:

  1.  
  2. int x = atoi(char);

Probably better ways, but that's how I do it.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,850
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 754
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: pls help me to convert char to integer.

 
1
  #3
Feb 16th, 2006
Getting the numeric value of a single character is simple:
  1. char lang[2][4] = {"en","fr"};
  2. int i = lang[0][0];
You're confused with the distinction between value and representation. The values of lang[0][0] and i are identical. The only difference is how they're printed, much like octal, decimal, and hexadecimal. The values are the same, but the representation is different. That's why you can do this:
  1. printf ( "%c\n", i ); /* Print i as a character */
  2. printf ( "%d\n", i ); /* Print i as an integer */
>int i = sprintf("%d",ch);
This is not how sprintf works. Until you have a stronger foundation in the idioms of C, you should refrain from guessing and use a good reference.

>int x = atoi(char);
That's broken, and you should know better. atoi takes a null terminated string. If you pass it a single character that doesn't have the value of 0, you invoke undefined behavior. Not to mention that atoi will fail and return 0 if the character is non-numeric, which in my world, 'e' most certainly is.

Here's a nice little guideline for both of you: If you don't know, go find out. In C and C++ there's no margin for error, so a guess is just asking for trouble.
New members chased away this month: 4
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 21391 | Replies: 2
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC