hello my question is that i want 16 Byte hex address by combining to 8 byte hex values as follows:
unsigned long addr; unsigned char buf[3]; buf[0]=0x81; // msb buf[1]=0x23; // lsb ... printf("%lx", &addr); //output: addr = 0x8123
i want to retrieve from this array one hex address into 'addr' variable. my output is: addr = 0x8123.
thanks in advance
"i want 16 Byte (sic?) hex address by combining to 8 byte (sic?) hex values"
Don't you mean a 16-bit (ie, 2-byte) value from two 8-bit (ie, 1-byte) values?!
Note that "hex" is irrelevant here - they are just values.
There are 3 approaches:
1. Use the standard 'C' shift operator;
2. Use a union;
3. Use a pointer, and cast it appropriately.
I think that the answers to your previous question can be applied here as well?
sorry, i meant 2 Byte. when i applied the previous answer i got something else, all i want is to combine the 2 byte's into 1 address. 0x81 0x23 -> 0x8123.
unsigned char buf[4]; unsigned long addr; union split {unsigned int word; struct {unsigned char hi;unsigned char lo;} bytes;}; union split newcount; buf[0] = 0x81; buf[1] = 0x23; newcount.bytes.hi = buf[0]; newcount.bytes.lo = buf[1]; addr = newcount.word; // output: addr = 0x8123
.
i want to retrieve from this array one hex address into 'addr' variable.
why not a struct and all is simple.
I see the array named 'buf' but if an array is chosen for serial receive, you can typecast it in the loader
Erik
all i want is to combine the 2 byte's into 1 address. 0x81 0x23 -> 0x8123.
i thing your need is combine two 8bits into 16bit word am i right? ok do like this
short total; char val1; char val2; total=val2; total= total<<7; total|=val1;
-KTV
nice. but it's working this way:
short total; buf[0] = 0x81; buf[1] = 0x23; total = buf[0]; total = total << 8; total |= buf[1]; // total = 0x8123
no need for struct or union (complicated...) thanks.
Why not just simplify it to:
short total; buf[0] = 0x81; buf[1] = 0x23; total = buf[0]<<8 | Buf[1]; // total = 0x8123
John, you beat me to it!
Hey! You are the forth "malund" today! One big happy family? Brace yourself, rahib kalib!
Anyone know what the collective noun for a group of Malunds is?
Maybe a project, or a billow or an asylum.
How about using the first letter of each family member's name ? if you then add an 'A', you get JEANS
:-)
... that my responses "you should not start making a Mars lander before you have mastered 'blinky'" offends you, I pity you
"... that my responses "you should not start making a Mars lander before you have mastered 'blinky'" offends you, I pity you"
What thread are you commenting? You don't have any previous comment that anyone have reacted to.