Click to See Complete Forum and Search --> : how to get the last 2 digits of an hex value


sllai
July 1st, 2002, 09:06 PM
Hi

I hv one hex value
0xFFFFFFAA in an array names Value[0]

now I try to get the AA value only
how can I do it

can someone provide some clue

Thanks
sllai

stober
July 1st, 2002, 10:40 PM
What data type is Value[0]? If it is char*, then:
int len = strlen(Value[0]);
char *last_two_digits = &value[0][len-2];

sllai
July 2nd, 2002, 01:18 AM
Thanks

I try to combine two value

value[0] = 0x000000AA
value[1] = 0x000000BB

to become a new value
new = 0xAABB0000000000

how can I implement it

thanks in advanced

cup
July 2nd, 2002, 03:04 AM
I hope you are doing this in C and not C++. new is a reserved word in C++.

new = (value[0] << 24) | (value[1] << 16);

dude_1967
July 2nd, 2002, 03:30 AM
You must mask and possibly shift your data in value[], as cup has suggested. Familiarize yourself with these types of operations.

The answer to your direct question regarding the "AA" part of "FFFFFFAA" (written in plain C) is:

unsigned long int dword_aa_part;

<maybe other lines of code>...

dword_aa_part = ((unsigned long int) value[0]) & 0x000000FF;

This was an easy example, since merely a mask was needed.
Some of your goals will involve masking and subsequently shifting by 8, 16, or 24 (again, see the example from cup). Typical masks will be 0xFF000000, 0x00FF0000, 0x0000FF00, and 0x000000FF. The right shifft operator >> and left shift operator << will be needed (see example from cup).

Please watch out for the storage convention of the underlying compiler. The calculations will always work out, however the view of the raw data, for example in a memory watch-window, could look "byte-swapped" to the untrained eye. You will get it after some training. Be sure to use a good debugger.

Good luck! Chris.

:)