Well, you could probably find code floating aroung the web somewhere, or
figure how to use some C standard library functions, but it's easy to do,
and, once you get the picture, it will be with you forever.
First of all, not all systems use ASCII representations (although, these
days, almost all do), so I wouldn't recommend anything that has ASCII
programmed into the algorithm.
I will present what I think is a simple way to do it. You can try to
think of more elegant methods if you are really interested.
There are three cases of characters for hex digits that we must consider:
'0' to '9'
'a' to 'f'
'A' to 'F'
(since hex digits may be represented by either upper- or lower-case a-f)
I'll give you words; you write code:
If the ASCII character is
(greater than or equal to '0') and (less than or equal to '9') then
the decimal value can be obtained by subtracting the character '0'.
You can see that '0' becomes 0,
'1' becomes 1,
etc.
Now if the ascii character is
(greater than or equal to 'a') and (less than or equal to 'f') then
the decimal value can be found by subtracting 'a' and adding 10.
That is 'a' becomes 10
'b' becomes 11
etc
You could convert 'A' - 'F' to lower case (using standard library function
tolower()), then use the 'a' - 'f' test, or you could continue as above
If the ascii character is
(greater than or equal to 'A') and (less than or equal to 'F') then
the decimal value can be found by subtracting 'A' and adding 10.
That is 'A' becomes 10
'B' becomes 11
etc.
So you could write a function, say atohex(), to convert as shown.
Then you could have something like
Code:
INSLength = (16 * atohex(RawPacket[ 8])) + atohex(RawPacket[ 9];
Dave