PDA

Click to See Complete Forum and Search --> : Converting Integers to string


ycsim
Apr 24th, 2000, 04:51 PM
I know this is a stupid question but i am quite new to C. How do I convert Integers to Array of Char???

I know how to convert from an Array of Chars using the sscan

Fox
Apr 24th, 2000, 05:10 PM
You can use sprintf...


int a;
char text[255];

sprintf( text, "%d", a );

Apr 25th, 2000, 07:57 AM
Also, you can look up a function called "itoa".

[Edited by VirtuallyVB on 04-25-2000 at 09:00 PM]

Doomstar
May 6th, 2000, 12:35 AM
you should try an explicit conversion, the reason for this over atoi or itoa is that you can use it on allmost every possible variable. how it works:


int y;
CString z;

y = 4;
z = "2";

if (blnItoA == TRUE)
{
z = (CString) y;
// Now z == "4"
}
else
{
y = (int) z;
// Now y == 2
}


that shouldn't be too tough...

[Edited by Doomstar on 05-06-2000 at 01:35 PM]

[Edited by Doomstar on 05-06-2000 at 01:36 PM]

HarryW
May 6th, 2000, 03:35 AM
I'm sorry, I know some people hate it when people ask questions half way through a thread, but what's the difference between (int) and static_cast<int> ?

In the C++ book I've got it says you should use the latter in C++, but doesn't really give a reason.

Doomstar
May 6th, 2000, 04:52 AM
eh, well, i don't know the difference. in fact, i've never heard of static_cast<int>, but i'm not doing this for that long either. just use the (int) method, that way you're sure it works (i use it all the time in VC++)