9

I need to convert decimal number stored in an int, to a array of bytes (aka stored in a unsigned char array).

Any clues?

0

5 Answers 5

12

Or if you know what you are doing:

int n = 12345;
char* a = (char*)&n;
Sign up to request clarification or add additional context in comments.

Comments

1

Simplest possible approach - use sprintf (or snprintf, if you have it):

unsigned char a[SOMESIZE]
int n = 1234;
sprintf( a, "%d", n );

Or if you want it stored in binary:

unsigned char a[sizeof( int ) ];
int n = 1234;
memcpy( a, & n, sizeof( int ) );

5 Comments

Warning: memcpy can produce unpredictable behavior if you don't account for endianness.
@Zarel The results will be entirely predictable, but perhaps unwanted.
"Unpredictable" in the sense that you won't be able to predict the results unless you know the endianness of the processor you run it on.
@Zarel: if you care about endianness just call hton() on your int before memcpy() and you get a fixed order (network order).
memcpy preserves the endianness; if you want to access back the data casting a to (int *), you can and will get the right result. If you "correct" the endianness while copying data, then you will need to "invert" it again before accessing the memory as if it would contain an int in the endianness of the machine (which is exactly what it happens: in memory there's no difference between data pointed by a and by &n, it changes only what "C" thinks they are)
1

This could work

int n=1234;    
const int arrayLength=sizeof(int);
unsigned char *bytePtr=(unsigned char*)&n;

for(int i=0;i<arrayLength;i++)
{
   printf("[%X]",bytePtr[i]);
}

Take care of order that depends on endianness

Comments

0

I understand the problem as converting a number to a string representation (as Neil does).

Below is a simple way to do it without using any lib.

int i = 0;
int j = 0;
do {a[i++] = '0'+n%10; n/=10;} while (n);
a[i--] = 0;
for (j<i; j++,i--) {int tmp = a[i]; a[i] = a[j]; a[j] = tmp;}

The question probably needs some clarification as others obviously understood you wanted the underlying bytes used in internal representation of int (but if you want to do that kind of thing, you'd better use some fixed size type defined in instead of an int, or you won't know for sure the length of your byte array).

Comments

0

Warning: untested code.

This should be an endianness-agnostic conversion. It goes from low to high. There's probably a more efficient way to do it, but I can't think of it at the moment.

#include <limits.h> // CHAR_BIT, UCHAR_MAX

int num = 68465; // insert number here
unsigned char bytes[sizeof(int)];
for (int i=0; i<sizeof(int); i++)
{
    bytes[i] = num & UCHAR_MAX;
    num >>= CHAR_BIT;
}

I'm posting this mostly because I don't see another solution here for which the results don't change depending on what endianness your processor is.

1 Comment

But perhaps the user wants them to change - to maintain the same byte order as in the original int. Also, your array sizing and loop control calculation is wrong.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.