# Thread: long integers and byte order

1. Thanks grumpy and sorry about the conversion error for 6844.

My long int is always under 65x10^6 (e.g. less than 80000000) so I assume I don't need to worry about signs.

I settled on a method that does not invoke dividing to shift out the bytes and it seems work. This is because although the long int bytes are stored in reversed order, the nibbles are in proper order.
The routine which I have been using and seems to work is shown below.
I think it is a bit verbose-I thought that perhaps C and its functions provided a "nice" way to simply convert long to hex in a buffer.

Thanks again-
Fritz

Code:
```void cvt_long_to_hex(long int lngint, char hexout[4])
{
long int *liptr;
char *cptr;
liptr=&lngint;
cptr=(char*)liptr;
hexout[0]=cptr[3];
hexout[1]=cptr[2];
hexout[2]=cptr[1];
hexout[3]=cptr[0];
} //end cvt_long_to_hex```

2. Originally Posted by sonnichs
Thanks grumpy and sorry about the conversion error for 6844.

My long int is always under 65x10^6 (e.g. less than 80000000) so I assume I don't need to worry about signs.

I settled on a method that does not invoke dividing to shift out the bytes and it seems work. This is because although the long int bytes are stored in reversed order, the nibbles are in proper order.
The routine which I have been using and seems to work is shown below.
I think it is a bit verbose-I thought that perhaps C and its functions provided a "nice" way to simply convert long to hex in a buffer.

Thanks again-
Fritz

Code:
```void cvt_long_to_hex(long int lngint, char hexout[4])
{
long int *liptr;
char *cptr;
liptr=&lngint;
cptr=(char*)liptr;
hexout[0]=cptr[3];
hexout[1]=cptr[2];
hexout[2]=cptr[1];
hexout[3]=cptr[0];
} //end cvt_long_to_hex```
Your hexout array needs to be "unsigned char" ... although you never hit the sign bit in the integer value you almost certainly will hit it in one or more of the extracted bytes.

3. Hmmmm-the reason I did not want to use unsigned was because the serial port routines on this processor complain about it. However, when I set my long int to X'11FFFFFFFF', the result in the buffer is the same.

4. Originally Posted by sonnichs
Hmmmm-the reason I did not want to use unsigned was because the serial port routines on this processor complain about it. However, when I set my long int to X'11FFFFFFFF', the result in the buffer is the same.
If you are pulling bytes directly out of the buffer and making no other use of them signed vs unsigned may not matter... however if you were looking for the specific value of one byte it could magically be transformed to a negative number...

5. "magically be transformed to a negative number"
Yuk-now I remember while I used to have the patience to suffer with assembler!