Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can cast a Char to an int and the int will hold the ascii value of the char. For example, '0' will be 48, '5' will be 53. The letters occur higher up so 'a' will be cast to 97, 'b' to 98 etc. So knowing this you can take the int value and subtract 48, if the result is greater than 9, subtract another 39. Then char 0 will have been turned to int 0, char 1 to int 1 all the way up to char a being set to int 10, char b to int 11 etc.</p> <p>Next you will need to multiply the value of the first by 16 and add it to the second to account for the bit shift. Using your example of 2a.</p> <p>char 2 casts to int 50. Subtract 48 and get 2. Multiply by 16 and get 32. char a casts to int 97. Subtract 48 and get 49, this is higher than 9 so subtract another 39 and get 10. Add this to the end result of the last one (32) and you get 42.</p> <p>Here is the code:</p> <pre><code>int HexToInt(char hi, char low) { int retVal = 0; int hiBits = (int)hi; int loBits = (int)low; retVal = Convert(hiBits) * 16 + Convert(loBits); return retVal; } int Convert(int in) { int retVal = in - 48; //If it was not a digit if(retVal &gt; 10) retVal = retVal - 7; //if it was not an upper case hex didgit if(retVal &gt; 15) retVal = retVal - 32; return retVal; } </code></pre> <p>The first function can actually be written as one line thus:</p> <pre><code> int HexToInt(char hi, char low) { return Convert((int)hi) * 16 + Convert((int)low); } </code></pre> <p>NOTE: This only accounts for lower case letters and only works on systems that uses ASCII, i.e. Not IBM ebcdic based systems.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload