Newbie:24 bit-----> 3byte

P

Prakruthi

Hi,

I have a 24 bit unsigned integer array. I would like to convert it into
a 3 byte output value.
Can someone please tell me how to go about doing this. I m a newbie to
C programming and have failed in trying to implement this conversion.

Thanks,

Prakruthi
 
W

Walter Roberson

Prakruthi said:
I have a 24 bit unsigned integer array. I would like to convert it into
a 3 byte output value.
Can someone please tell me how to go about doing this. I m a newbie to
C programming and have failed in trying to implement this conversion.

I'm not certain what you are specifying. Are you indicating that
you are working on a system on which unsigned ints happen to occupy
24 bits? And on that system, char is exactly 8 bits long?

Are you indicating that you have an array of two unsigned integers,
each of which could store 16 bits, but of which you are using 24
of the bits -- e.g., something similar to 0X YZ and you want to
extract the X Y and Z ?

Are you indicating that you have a large number of 24 bit unsigned
integers, and that there is some formulae that you have to will
operate on the list of integers and produce a 24 bit answer
that you then want to convert to bytes? Something akin to running
a CRC algorithm over the whole array and producing an output from that?

When you extract the 3 bytes from the 24 bit integer, do you want
the output bytes to be "first byte of the integer, then
second byte of the integer, then third byte of the integer" ?
Or do you want the output bytes to be "most significant 8 bits
of the integer, then next most significant 8 bits of the integer,
then least significant 8 bits of the integer" ? To clarify this,
when you have an integer as a whole, sometimes the bytes are
stored internally in an order you might not at first expect.
For example, X * 65536 + Y * 256 + Z might get stored in memory
in the byte order X then 0 then Z then Y, X0ZY so when you say you
want to extract bytes from the integer, we need to know whether
you want the order they occur in memory (e.g., X 0 Z) or if you
want the order by numeric signficance (e.g., X Y Z)
 
P

Prakruthi

Dear Walter,
integers, and that there is some formulae that you have to will
operate on the list of integers and produce a 24 bit answer
that you then want to convert to bytes?

I`m sorry for not being clear about the issue at hand.
'I have a large number of 24bit unsigned integers which i want to
convert to 3 bytes in the order of numeric significance
- most significant bits, next most significant bits and then the least
significant bits.

Hope its more clear now.
Looking forward for your reply.

Regards,
Prakruthi
 
M

Mark McIntyre

Dear Walter,

integers, and that there is some formulae that you have to will
operate on the list of integers and produce a 24 bit answer
that you then want to convert to bytes?

I`m sorry for not being clear about the issue at hand.
'I have a large number of 24bit unsigned integers which i want to
convert to 3 bytes in the order of numeric significance
- most significant bits, next most significant bits and then the least
significant bits.

I think whats troubling people is that a 24-bit unsigned integer is
ALREADY three bytes of data on any machine with 8-bit bytes.

The bit-order thing machine-dependent and sounds suspiciously like
network address mapping. On some hardware it may already be in that
order. On other hardware you should probably be calling networking
functions to put the bytes in the right order. If all else fails, walk
down your bits arranging them in the order you want.
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
P

pete

Prakruthi said:
'I have a large number of 24bit unsigned integers which i want to
convert to 3 bytes in the order of numeric significance
- most significant bits, next most significant bits and then the least
significant bits.

/* BEGIN new.c */

#include <stdio.h>

#define BYTES 3
#define NUMBER 0x123456

int main(void)
{
long unsigned number = NUMBER;
char array[BYTES];
size_t index;

for (index = 0; index != sizeof array; ++index) {
array[BYTES - index - 1]
= (char)((number >> 8 * index) & 255);
}
printf("0x%lx\n\n", number);
for (index = 0; index != sizeof array; ++index) {
printf("0x%x\n", (unsigned)array[index]);
}
return 0;
}

/* END new.c */
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top