Integer to String (in 4 bytes)

Started by
6 comments, last by snowball123 22 years, 1 month ago
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; ...
Advertisement
If itoa good enough?
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
union ByteInt{  unsigned integer i;  unsigned char    c;};ByteInt bi;bi.i = 888888;cout << bi.c[0] << endl; 

Enjoy.

[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! ]
Thanks to Kylotan for the idea!
What exactly is the end result you are looking for?

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

iInteger = 8888;
then
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.

Seeya
Geek
char bytestring[3];
int somenumber = 3434943;
memcpy(bytestring,&somenumber,sizeof(bytestring));

[edited by - barazor on March 17, 2002 10:31:47 PM]
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!
yeah, just postin a different way of doin it
if i was working with this kinda situation, id use the union method too
Hi,


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.

Thanks

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

This topic is closed to new replies.

Advertisement