Convert an int to char array rep
-
Hi, Is there a more efficient way of converting an integer value into a char array representation. Hence, the char array would be set to size four because there are 4 bytes in an integer.
Do you mean converting an integer value into a string? If so, that's in the CP C++ FAQ here[^] -- Ian Darling "The moral of the story is that with a contrived example, you can prove anything." - Joel Spolsky
-
Hi, Is there a more efficient way of converting an integer value into a char array representation. Hence, the char array would be set to size four because there are 4 bytes in an integer.
Alternatively (to Ian's suggestion) if you mean to treat an integer as a four byte character array you can do so by casting: int i = getValue(); char* p = reinterpret_cast(i); However, that is a pretty scary thing to do, since obviously the string will not be guaranteed to be null terminated (unless i&0xFF is 0 on a big endian machine, or i&0xFF000000 is zero on a little endian machine, and also assuming that sizeof(int) == 4) Not recommended. Perhaps you could tell us what you are actually trying to do.
-
Do you mean converting an integer value into a string? If so, that's in the CP C++ FAQ here[^] -- Ian Darling "The moral of the story is that with a contrived example, you can prove anything." - Joel Spolsky
-
Alternatively (to Ian's suggestion) if you mean to treat an integer as a four byte character array you can do so by casting: int i = getValue(); char* p = reinterpret_cast(i); However, that is a pretty scary thing to do, since obviously the string will not be guaranteed to be null terminated (unless i&0xFF is 0 on a big endian machine, or i&0xFF000000 is zero on a little endian machine, and also assuming that sizeof(int) == 4) Not recommended. Perhaps you could tell us what you are actually trying to do.
-
Hi, Is there a more efficient way of converting an integer value into a char array representation. Hence, the char array would be set to size four because there are 4 bytes in an integer.
Something like this comes to mind:
int nNum;
char cBytes[4];cBytes[0] = (nNum >> 24) & 0xff;
cBytes[1] = (nNum >> 16) & 0xff;
cBytes[2] = (nNum >> 8) & 0xff;
cBytes[3] = nNum & 0xff;
Five birds are sitting on a fence. Three of them decide to fly off. How many are left?
-
I'm trying to convert an integer value into an array of bytes. Char array is used cause it's exactly one byte. I have library func which sends bytes over a stream if given a pointer to char array head.
There are couple of issues you should be concerned with here. First of all, you need to send the integer in a representation that all readers will accept. As you probably know, different processor achitectures represent integers in different ways, consequently, most likely the best representation would be a simple ASCII stringL void convertMe(int i) { char s[MAX_CHAR]; itoa(i, s, 10); sendBytes(s); } where sendBytes is your sending function. If you don't care about endian-ness, you could also send the bytes directly like this: sendBytes(reinterpret_cast(&i), sizeof(i)); However this assumes that sendBytes is not designed to send a string, but a block of memory (hence the second parameter which is the length of the block to send.) If it is designed to send strings it will fail quite often since any zero byte within the integer will terminiate the string.