int to char

This is a discussion on int to char within the C++ Programming forums, part of the General Programming Boards category; I wanna make an function that converts int to char..
but i just cant figure out how to go through ...

It would be easiest to use the standard functions to do the conversion - just use sprintf;

Code:

sprintf(b, "%i", i);

if I remember correctly.. I believe it's in the <cstring> header

I wanna make an function that converts int to char..

You do mean char* right? as in a string? because converting an int (which has a minimum range of -32,767 to 32,767) will take up to 6 characters to display (unless of course it(the int) is bigger than that, which it probably will be, since 16 bits is the *minimum* required by the standard)

but i just cant figure out how to go through each number contained in an int..

like int a = 123;

use a pointer (maybe?) to point at each number at a time..

1
2
3
...

remember, the int is not a string of chars. It's binary; i.e. it looks like this if it's 123;
01111011
altho it will probably also have a bunch of zeros in front of it (likely either 8 or 24), wheras the ASCII characters 1 2 and 3 look like;
00011111 00100000 and 00100001
each character is one byte (8 bits).

But if you really want to do it yourself.. I would suggest starting with the 1's place and moving up to the top, remembering each digit as you go, and then create a string and put the char values into it.

[edit]
lol.. by the time I posted this there was already a bunch of other replies
actually if you follow Hammer's link above you will also end up at a description of sprintf
[/edit]

Oh yeah.. stringstream..

totally forgot that (stringstream) one haven't done too much with it myself so the only one I've actually used is sprintf.
Anyway, I was just gonna post, I was bored, and the idea of writing a itoa sounded kinda fun, so I did...

goes to show what someone can accomplish when they're bored.. that was pretty fun, actually.. thx for the challenge Luigi
plus from the looks of it it should even work whether the int is 16 or 32 bit.. tell me what you think?
man I wish I could get my bitmap loading function working as good as this turned out