Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Casting

Status
Not open for further replies.

PrograMan

Programmer
Jul 21, 2001
59
US
How do you cast an integer into a char array and vice versa?
 
I wonder why everyone's asking this for the last few days?? It ahs been answered in most of the other C/C++ forums. The easiest way is to use sprintf and atoi...

#include<stdio.h>
#include<stdlib.h>

int main()
{
char array[] = &quot;1234&quot;;
int num = 4321;

sprintf(array, &quot;%d&quot;, num); /* int to char array */
num = 0;
num = atoi(array); /* char to int */

return 0;
}
Ankan.

Please do correct me if I am wrong. s-)
 
Um, that's a nice bit of code there, but I was talking about C++, not C.
 
>Um, that's a nice bit of code there, but I was talking about C++, not C.

*ouch*

The functions you are looking for are _itoa() and atoi().
BTW: This is no &quot;cast&quot; but a &quot;conversion&quot;.
 
&quot;Um, that's a nice bit of code there, but I was talking about C++, not C.&quot;

If you knew.... why'd you ask?

C++ can do all that C can do (plus more)

Matt

 
Sorry, didn't mean to sound like that. I was frustrated at the time.

BTW: is there a header that I would need to #include in the code to do the _itoa() and _atoi()?
 
Heh, never mind. My bad for the stupid question.
 
&quot;Um, that's a nice bit of code there, but I was talking about C++, not C.&quot; :-I ??

&quot;C++ can do all that C can do (plus more)&quot; :) Exactly.

Morever, there can be many ways to do the same thing. And you are welcome not to use any code you don't like.

Never mind. :) (-: :) (-:

&quot;is there a header that I would need to #include in the code to do the itoa() and atoi()?&quot;
Its <stdlib.h> Ankan.

Please do correct me if I am wrong. s-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top