Jump to content
  • Advertisement


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


Integer to String (in 4 bytes)

This topic is 5967 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
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
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
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
yeah, just postin a different way of doin it
if i was working with this kinda situation, id use the union method too

Share this post

Link to post
Share on other sites

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

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!