system
September 12, 2012, 7:44pm
1
Hello all. Little guidance needed on converting a HEX string for example char str[5] contains BE 1F A8 13. Am wanting to add the 4 bytes together and place the answer into a unsigned long. Would a simple for loop work for this with the += compound operator?
Many thanks as always.
system
September 12, 2012, 7:46pm
2
Would a simple for loop work for this with the += compound operator?
By itself, no it wouldn't.
But some left shifts or some multiplies would sort it out.
Edit:
BE 1F A8 13.
Do you mean you want the sum of those values (0x198) in a 32 bit variable, or the value 0xbe1fa813 in a 32 bit variable?
Do you know the endianess of the input value?
uncompiled untested
unsigned long var = 0;
for int (int i = 0; i < 4; ++i) {
var <<= 8;
var+= array [i];
}
system
September 12, 2012, 7:58pm
3
Do you mean you want the sum of those values (0x198) in a 32 bit variable, or the value 0xbe1fa813 in a 32 bit variable?
Hello mate, yes i want the sum 198 hex, 408 DEC to go into a 32 bit variable.
system
September 12, 2012, 8:05pm
4
In which case the same loop without the shift.
You'll also need to cast the "char" to "unsigned char"
You'll also have at most ten bits of data in a 32 bit variable.
system
September 12, 2012, 8:09pm
5
I notice you use use
var <<= 8;
does this mean the unsigned long variable fills from right to left?
Sorry just noticed your last post lol.