i want to split a 29 bit value into bytes ? how can i accomplish this in C?
unsigned long value = 0x18FEF512;
I need the output like this val=0x18, val1=FE, val2=F5 , val3=12.
How to do this using bit twidlling?
i want to split a 29 bit value into bytes ? how can i accomplish this in C?
unsigned long value = 0x18FEF512;
I need the output like this val=0x18, val1=FE, val2=F5 , val3=12.
How to do this using bit twidlling?
try this:
unsigned long value = 0x18FEF512;
unsigned char values[4];
values[0] = value;
values[1] = value >> 8;
values[2] = value >> 16;
values[3] = value >> 24;
printf("%02X %02X %02X %02X\n",values[0],values[1],values[2],values[3]);
Edit per chux comment : this mixes a little C and C++ syntax - see below
The direct way is to put your 29 bit in a long
or unsigned long
and do bit shifting :
unsigned long value = 0x18FEF512;
unsigned char val0, val1, val2, val3;
val0 = (unsigned char) (value & 0xFF);
value >>= 8;
val1 = (unsigned char) (value & 0xFF);
value >>= 8;
val2 = (unsigned char) (value & 0xFF);
value >>= 8;
val3 = (unsigned char) (value & 0xFF);
If you have to do many times, if can be useful to first determine endianness and then directly load bytes. For endianness :
int i = 1;
char *ix = (char *) &i;
bool bigendian = (*ix == 0);
Then
char *bytes = (char *) &value;
if (bigendian) {
val3 = bytes[0];
val2 = bytes[1];
val1 = bytes[2];
val0 = bytes[3];
}
else {
val0 = bytes[0];
val1 = bytes[1];
val2 = bytes[2];
val3 = bytes[3];
}
Edit
The above mixed C++ syntax by declaring variables all along the code when C wants them at beginning, and using unneeded casts
Here is the same as true C :
#include <stdio.h>
int main() {
unsigned long value = 0x18FEF512;
unsigned char val0, val1, val2, val3;
int i = 1;
char *ix = (char *) &i;
char bigendian = (*ix == 0);
char *bytes = (char *) &value;
val0 = value & 0xFF;
value >>= 8;
val1 = value & 0xFF;
value >>= 8;
val2 = value & 0xFF;
value >>= 8;
val3 = value & 0xFF;
printf("Bit shifting : %x %x %x %x\n", val0, val1, val2, val3);
value = 0x18FEF512;
printf("%System is %s endian", bigendian ? "big" : "little");
if (bigendian) {
val3 = bytes[0];
val2 = bytes[1];
val1 = bytes[2];
val0 = bytes[3];
}
else {
val0 = bytes[0];
val1 = bytes[1];
val2 = bytes[2];
val3 = bytes[3];
}
printf("Endian aware byte access : %x %x %x %x\n", val0, val1, val2, val3);
}
What you actually were asking is how to print string
-segments of a string
-representation of a unsigned long
value, this can be achieved in numerous ways.
At first, you will need to get the string-representation of your integral, have a look at printf and the %X
format - this will get you an idea of what needs to be done (tip : there is a similar function to printf which stores the result into your own field instead of printing it to stdout)
Now you will need to split that resulting string into pieces, strtok will do exactly that.
With the resulting tokens you can now fill up a new char
-array and finally print it.
Good luck with your assignment, you are now equipped with the necessary knowledge.
One more thing : this has nothing to do with "bit twidding" or bit shifting. At all. Maybe there ARE ways to calculate the integral segments which are equal to the string-segments but i highly doubt anyone would willingly do the math behind it - that'd basically be "re-inventing the wheel" which doesnt even serve educational purposes ...