473,387 Members | 1,504 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Confusing itoa and atoi

Hi,

I have a char* that holds an ascii character in its first element (at
least I think that's what it holds because when I print it, it prints
weird characters). I need to convert this into an integer and so I
thought the following might work:

char *somevar ... // somevar holds a value in somevar[0] and is null
terminated i.e. somevar[1] = '\0'

int a = atoi(somevar);

However, this is not working. If I try:

int a = (int) somevar[0]; // this works

My second problem is that I now need to convert this "integer value"
that I just got into a char* to be passed onto some funtion for printing
it out as a string. So I do:

char* b = malloc(sizeof(char)*SOME_LENGTH);

b = itoa(a, b, 1);

I then print this out. This seems to work fine, but is there a simpler
way of doing this? Thanks :)

Sona
Nov 13 '05 #1
2 6650
Sona <so**********@nospam.com> writes:
I have a char* that holds an ascii character in its first element (at
least I think that's what it holds because when I print it, it prints
weird characters).
ASCII characters tend to be things like `a' or `6' or new-line or
tab. I can't see how printing weird characters indicates that
something is ASCII.
I need to convert this into an integer and so I thought the
following might work:

char *somevar ... // somevar holds a value in somevar[0] and is null
terminated i.e. somevar[1] = '\0'

int a = atoi(somevar);
atoi() expects its input to be something like "123", that is, a
string of digits. Weird characters are probably not digits.
However, this is not working. If I try:

int a = (int) somevar[0]; // this works
You shouldn't even need the cast to int.
My second problem is that I now need to convert this "integer value"
that I just got into a char* to be passed onto some funtion for
printing it out as a string. So I do:

char* b = malloc(sizeof(char)*SOME_LENGTH);
sizeof(char) is guaranteed to be 1, so there's no point in
writing it.
b = itoa(a, b, 1);


There is no itoa() function in standard C. Use sprintf() for
portability.
--
Go not to Usenet for counsel, for they will say both no and yes.
Nov 13 '05 #2
On Tue, 23 Sep 2003, Sona wrote:
Hi,

I have a char* that holds an ascii character in its first element (at
least I think that's what it holds because when I print it, it prints
weird characters). I need to convert this into an integer and so I
thought the following might work:
Whoa! There is a few problems here. I think you need to take a moment and
understand the terminology you are using. Your reasoning as to why the
first character that your pointer to char points at is ASCII makes no
sense. ASCII is a system for representing a character set. It just states
that certain binary values will be treated as certain characters. For
example, the binary value 48 or 0x30 will be the character '0'. The fact
that printing the string prints 'weird characters' means nothing.
char *somevar ... // somevar holds a value in somevar[0] and is null
terminated i.e. somevar[1] = '\0'
At this point you are telling me that somevar[1] holds a null character.
This means that somevar is a string. Doesn't mean it is printable. The
value in somevar[0] must be printable if you want to treat it as a string
you can print.
int a = atoi(somevar);
This will work if somevar[0] is in the range '0' to '9' and it will set a
to 0 through 9, respectively.
However, this is not working. If I try:

int a = (int) somevar[0]; // this works
This will take the binary value of somevar[0] and convert it to an int
then save it in a. For example, if you are on a system that uses ASCII and
the value of somevar[0] is '0' then a will be 48. This is because ASCII
'0' is decimal 48.
My second problem is that I now need to convert this "integer value"
that I just got into a char* to be passed onto some funtion for printing
it out as a string. So I do:

char* b = malloc(sizeof(char)*SOME_LENGTH);
NOTE: sizeof(char) is always 1. So multiplying by sizeof(char) is
pointless.

You also want to be sure the malloc was successful.
b = itoa(a, b, 1);
The itoa function is not standard. Check the FAQ for this newsgroup. There
is a section on itoa and how you should use sprintf instead.
I then print this out. This seems to work fine, but is there a simpler
way of doing this? Thanks :)

Sona


If I understand you correctly, you have somevar[0] = some binary value and
you want to make a string (a) of that value. For example, if somevar[0]==3
then b[]="3". If this is the case, you are making this a LOT harder than
it has to be. Things like atoi, itoa and sprintf are for converting
multiple digit numbers. If you know the number is always one digit then
you can just use math and assignment operators.

--
darrell at cs dot toronto dot edu
or
main(){int j=1234;char t[]=":@abcdefghijklmnopqrstuvwxyz.\n",*i=
"iqgbgxmdbjlgdv.lksrqek.n";char *strchr(const char *,int);while(
*i){j+=strchr(t,*i++)-t;j%=sizeof t-1;putchar(t[j]);} return 0;}
Nov 13 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: Mike Moum | last post by:
I think there may be a bug in string.atoi and string.atol. Here's some output from idle. > Python 2.3.4 (#2, Jan 5 2005, 08:24:51) > on linux2 > Type "copyright", "credits" or "license()"...
5
by: Bansidhar | last post by:
atoi() function seems not to have any support for Hex, octal number. Usually when I read from a text file then it contain number like 0x232 etc. In this case atoi() fells. In case of itoa() there...
10
by: cai_rongxi | last post by:
I know how to write c code to realize atoi function, but I don't know how to do the opposit. Any sample code is appreciated.
11
by: rayw | last post by:
I'm pretty new to C, although I did do some years ago now. I've been told that itoa is no longer a standard function, and that the ato... functions - although in the std - are not recommended. ...
3
by: Tracey | last post by:
I'm learning C++ and was stuck when writing code to convert a negative decimal number string to a hexdecimal number. For example, suppose the function interface is defined as: int atoi( const...
24
by: Mark | last post by:
hi, all i want is a simple function that takes an int, and returns a char* so i tried char * itoa(int i) { char buff; return _itoa(i,buff,10); }
47
by: sudharsan | last post by:
Hi could you please explain wat atoi( ) function is for and an example how to use it?
4
by: Feanor | last post by:
I am running this in a small loop: char charStock = {recBuff, recBuff, recBuff}; for(i=0; i<3; i++) printf("%c", charStock); record.numInStock = atoi(charStock); printf(" %d\n",...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.