Splitting an Integer into 4 Bytes
-
how do you split an integer variable into 4 byte variables, I'm looking for a function to achieve this.
int i; byte b1,b2,b3,b4; b1 = Byte1(i); b2 = Byte2(i); b3 = Byte3(i); b4 = Byte4(i);
Can anyone help? Thanks -
how do you split an integer variable into 4 byte variables, I'm looking for a function to achieve this.
int i; byte b1,b2,b3,b4; b1 = Byte1(i); b2 = Byte2(i); b3 = Byte3(i); b4 = Byte4(i);
Can anyone help? ThanksMany way to do this. One brute force way is:
b1 = i & 0x000000ff;
b2 = (i & 0x0000ff00) >> 8;
b3 = (i & 0x00ff0000) >> 16;
b3 = (i & 0xff000000) >> 24;
[
](http://www.canucks.com)Sonork 100.11743 Chicken Little "You're obviously a superstar." - Christian Graus about me - 12 Feb '03 Within you lies the power for good - Use it!
-
how do you split an integer variable into 4 byte variables, I'm looking for a function to achieve this.
int i; byte b1,b2,b3,b4; b1 = Byte1(i); b2 = Byte2(i); b3 = Byte3(i); b4 = Byte4(i);
Can anyone help? Thanksnot a very safe-cast method but who cares it works:
int i = 12345; unsigned char byte1 = ((unsigned char*)&i)[0]; unsigned char byte2 = ((unsigned char*)&i)[1]; unsigned char byte3 = ((unsigned char*)&i)[2]; unsigned char byte4 = ((unsigned char*)&i)[3];
r -€
-
how do you split an integer variable into 4 byte variables, I'm looking for a function to achieve this.
int i; byte b1,b2,b3,b4; b1 = Byte1(i); b2 = Byte2(i); b3 = Byte3(i); b4 = Byte4(i);
Can anyone help? ThanksI will add this one: int i=12345; BYTE* ab = (BYTE*)&i; // use ab[0-3] Igor Green http://www.grigsoft.com/ - files and folders comparison tools
-
I will add this one: int i=12345; BYTE* ab = (BYTE*)&i; // use ab[0-3] Igor Green http://www.grigsoft.com/ - files and folders comparison tools
assume i is 2 bytes for simplicity. int i = 0x1234; byte c1,c2, then according to your logic, the output would be like this. c1 = 0x34; and c2 = 0x12; which is wrong.This is because the way it is stored in the memory. and if you consider for 32bit integer value, the error would just double. and according to PJ c1 = 0x12; & c2 = 0x34; here the output is correct, hope you got the picture now.