This topic is now archived and is closed to further replies.


Integer to String (in 4 bytes)

Recommended Posts

snowball123    122
Hi, I want to place an integer (4 bytes) in an array of characters. I''m trying to save space by not using sprintf. Does any one know how to do this? I tried using a function where the first 8 bits are placed in char, then use a bitwise operator to get the next 8 bits, so on ..... But it doesn''t work. My whole goal is to place int iInteger; char szArray [4]; iInteger = 88888; // whatever szArray [0] = iInteger; szArray [1] = iInteger >> 8; ...

Share this post

Link to post
Share on other sites
supergeek2k    122
What exactly is the end result you are looking for?

Are you trying to convert an integer to a string such that

iInteger = 8888;
szArray[] = "8888";


If so your shift is not going to work since it is grabbing 8 bits at a time, which equates to -128 - 127 for a char, and not a single digit.

And what exactly do you mean by save space by not using sprintf?

sprintf is probably your best bet as it gives you more control over the output format than itoa does.

Otherwise you want to perform a series of conditional divides to get your digits. Binary by definition is base 2 and you are trying to get a printable integer that is base 10, so shifting the bits wont work since no range of bits will give exactly 10 possible values.

Or are you just trying to break any integer into 4 8-bit values, and not really concerned about a printable string? If that is the case you are on the right track.


Share this post

Link to post
Share on other sites
barazor    122
char bytestring[3];
int somenumber = 3434943;

[edited by - barazor on March 17, 2002 10:31:47 PM]

Share this post

Link to post
Share on other sites
Oluseyi    2103
I will quietly gloat over the fact that my union method beats the pants off memcpy, unless you wanted a character conversion. Furthermore, it uses less memory as a union causes all its members to occupy the same space (size of the largest member).

If you did want a character conversion, then you suffer from an advanced form of "programmer''s delusion" in thinking you''ll gain anything (reasonable) over itoa or sprintf.

[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! ]
Thanks to Kylotan for the idea!

Share this post

Link to post
Share on other sites
snowball123    122

Oluseyi was right on. Nice Job!

The reason atoi or sprintf won't work is that a number like 88888 will take 5 bytes to store when it really only needs 4 bytes. (Remember, integer is just 4 bytes)

int i = 12345;

sprintf Function will store the integer i in 5 bytes, same with itoa.

I also got the bitwise operator to work (i admit your routine is better) but here it is

void IntToString (int iData, char *szString)
szString [3] = (char)iData;
szString [2] = iData >> 8;
szString [1] = iData >> 8;
szString [0] = iData >> 8;
szString [4] = '\0';

My reason for asking this question is to save space and standard length for accessing integer data for packets in WinSock (Multiplayer Stuff).

Also, I assume, Oluseyi, your routine would also work for floats too.


[edited by - snowball123 on March 17, 2002 12:03:04 AM]

Share this post

Link to post
Share on other sites