Answer the question
In order to leave comments, you need to log in
How to convert an unsigned number to 4 bytes?
The essence of the problem is that this must be done without the use of shifts and bitwise operations. I managed to quickly find an algorithm for converting a number into 2 bytes.
word x=8800; // = 0x2260
byte a=x/256;
byte b=x%256;
Answer the question
In order to leave comments, you need to log in
That's why institute education (programmer) is useless, because they force you to use division where you need to use shifts and binary operations.
On the subject, you think correctly:
low byte = x% 256
first byte = (x / 256)% 256
second byte = (x / (256 * 256))% 256
high byte = x / (256 * 256 * 256)
What language is it?
For C/C++
long val = 0x12345678;
printf("long: %ld\n", val);
printf("short: %d %d\n", ((short *)(&val))[0], ((short *)(&val))[1]);
printf("char: %d %d %d %d\n", ((char *)(&val))[0], ((char *)(&val))[1], ((char *)(&val))[2], ((char *)(&val))[3]);
union {
long l;
short s[2];
char c[4];
} val;
long val.l = 0x12345678;
printf("long: %ld\n", val.l);
printf("short: %d %d\n", val.s[0], val.s[1]);
printf("char: %d %d %d %d\n", val.c[0], val.c[1], val.c[2], val.c[3]);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question