Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » int to string (char) conversion in C

Credits go to LennyLen for helping out!
This thread is locked; no one can reply to it. rss feed Print
int to string (char) conversion in C
Bob Keane
Member #7,342
June 2006

Hello all. I am new to C programming and was wondering if it is possible to convert an integer value to char or string. I checked a book, but only found documentation to convert from string to integer. Thanks in advance.

By reading this sig, I, the reader, agree to render my soul to Bob Keane. I, the reader, understand this is a legally binding contract and freely render my soul.
"Love thy neighbor as much as you love yourself means be nice to the people next door. Everyone else can go to hell. Missy Cooper.
The advantage to learning something on your own is that there is no one there to tell you something can't be done.

X-G
Member #856
December 2000
avatar

A char is an integer type already.
sprintf

--
Since 2008-Jun-18, democracy in Sweden is dead. | 悪霊退散!悪霊退散!怨霊、物の怪、困った時は ドーマン!セーマン!ドーマン!セーマン! 直ぐに呼びましょう陰陽師レッツゴー!

ReyBrujo
Moderator
January 2001
avatar

int to char is done by simply casting:

int i = 10;
char c;
c = i;

Remember that chars can hold 256 numbers only. As for string, just use sprintf, like sprintf(buffer, "%d", i);, where buffer is a char array and i an integer. Remember to make the buffer as large as necessary.

--
RB
光子「あたしただ…奪う側に回ろうと思っただけよ」
Mitsuko's last words, Battle Royale

LennyLen
Member #5,313
December 2004
avatar

If you want to convert from an integer to a string with a specified radix, use itoa().

itoa()

Bob Keane
Member #7,342
June 2006

Thanks for the help. If I were adding the numeric value to the end of an existing string, would the format be:

char buffer[16} = "<some characters>";
int place;
char c;
c = sprintf(buffer, "%d", place);?

[/edit] I tried itoa, got an undefined reference to itoa.
[edit/]

By reading this sig, I, the reader, agree to render my soul to Bob Keane. I, the reader, understand this is a legally binding contract and freely render my soul.
"Love thy neighbor as much as you love yourself means be nice to the people next door. Everyone else can go to hell. Missy Cooper.
The advantage to learning something on your own is that there is no one there to tell you something can't be done.

LennyLen
Member #5,313
December 2004
avatar

Nah, just:

char buffer[16} = "<some characters>";
int place = 10;

sprintf(buffer, "%d", place);

Bob Keane
Member #7,342
June 2006

That worked. Thanks LennyLen and X-G.

[\edit] Oops, closed the thread too soon. It appears to be overwriting what is in buffer, as opposed to adding place to the end. Any ideas?
[\edit]

By reading this sig, I, the reader, agree to render my soul to Bob Keane. I, the reader, understand this is a legally binding contract and freely render my soul.
"Love thy neighbor as much as you love yourself means be nice to the people next door. Everyone else can go to hell. Missy Cooper.
The advantage to learning something on your own is that there is no one there to tell you something can't be done.

LennyLen
Member #5,313
December 2004
avatar

Quote:

I tried itoa, got an undefined reference to itoa

You need to add the following to the top of your code:

#include <stdlib.h>

Bob Keane
Member #7,342
June 2006

<stdlib.h> was included. Is it recent? Maybe my compiler does not support it yet.

By reading this sig, I, the reader, agree to render my soul to Bob Keane. I, the reader, understand this is a legally binding contract and freely render my soul.
"Love thy neighbor as much as you love yourself means be nice to the people next door. Everyone else can go to hell. Missy Cooper.
The advantage to learning something on your own is that there is no one there to tell you something can't be done.

LennyLen
Member #5,313
December 2004
avatar

It's not new, but it's not an ANSI C funtion either, though most compilers support it. Which are you using?

The following compiled fine for me using GCC.

1#include <stdio.h>
2#include <stdlib.h>
3 
4char buffer[20];
5int n;
6 
7int main() {
8
9 n = 25;
10
11 itoa(n, buffer, 2);
12 printf("%d in binary is %s\n", n, buffer);
13
14 itoa(n, buffer, 16);
15 printf("%d in hexadecimal is %s\n", n, buffer);
16
17 return 0;
18
19}

Bob Keane
Member #7,342
June 2006

I am using gcc 4.0.2. I copied the test program you submitted and compiled, still having problems with the itoa function. Are you coding in C or C++? I coded in C.

By reading this sig, I, the reader, agree to render my soul to Bob Keane. I, the reader, understand this is a legally binding contract and freely render my soul.
"Love thy neighbor as much as you love yourself means be nice to the people next door. Everyone else can go to hell. Missy Cooper.
The advantage to learning something on your own is that there is no one there to tell you something can't be done.

LennyLen
Member #5,313
December 2004
avatar

I compiled as C. I'm currently using GCC 3.4.2, so perhaps it's been removed in later versions.

CGamesPlay
Member #2,559
July 2002
avatar

New rule: no creating new programming forum threads until you read the thread list!

Is this week the printf assignment at the college that you three attend?

http://www.allegro.cc/forums/thread/588773
http://www.allegro.cc/forums/thread/588701

Not that I'm being hostile or anything.

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

Bob Keane
Member #7,342
June 2006

CGamesPlay: My origional question was how to convert an int to char or string. To get back on topic, I tried sprintf, the new characters overwrote what was in the target string. Is there any way to add the characters to the end of the string, or did I do something wrong?

By reading this sig, I, the reader, agree to render my soul to Bob Keane. I, the reader, understand this is a legally binding contract and freely render my soul.
"Love thy neighbor as much as you love yourself means be nice to the people next door. Everyone else can go to hell. Missy Cooper.
The advantage to learning something on your own is that there is no one there to tell you something can't be done.

LennyLen
Member #5,313
December 2004
avatar

#include <stdio.h>
#include <string.h>

char buffer[20], buffer2[20];

int main() {
  
     n = 25;
     strcpy(buffer, "stuff");
     sprintf(buffer2, "%d", n);
     strcat(buffer, buffer2);
  
     return 0;

}

Bob Keane
Member #7,342
June 2006

That was it. Thanks.

By reading this sig, I, the reader, agree to render my soul to Bob Keane. I, the reader, understand this is a legally binding contract and freely render my soul.
"Love thy neighbor as much as you love yourself means be nice to the people next door. Everyone else can go to hell. Missy Cooper.
The advantage to learning something on your own is that there is no one there to tell you something can't be done.

Go to: