HEX_ to_DEC_for those who lookin

Just putting out the HEX to DEC code here for those who lookin.

There was a time I was lookin for this one, end up I wrote it.

Say thanks if you use it.

int HEX_to_DEC(int a, int b) //HEX to DEC - s returnom…
{

if ( ‘0’ <= a && a <=‘9’)
{
a = a-48;
} //HEX to DEC
if( ‘A’ <= a && a <=‘F’)
{ a = a-55;}

if ( ‘0’ <= b && b <=‘9’)
{
b = b-48;
} //HEX to DEC
if( ‘A’ <= b && b <=‘F’)
{ b = b-55;}

return a*16 + b;

}

What advantage does this have over strtol()? That's what I've been using in my sketches for turning hexadecimal strings into bytes.

so in this code you are using an int, but you should be using char

I dont get why you dont just do a -= 48; or a -= 55;

a*16 should be a << 4

you don’t accommodate for ‘a’ or ‘f’ (lowercase)

Not only that, but the input should be a string, not 2 integers.

something like this, not tested…(got to go to work, ran out of time)

int hexToDec(char* hex){
  int output = 0;
  signed char sign = 1;
  byte start = 1;
  if(hex[0] == '0' && (hex[1] == 'x' || hex[1] == 'X'))
    start = 3;
  if((hex[start - 1] >= '8' && hex[start - 1] <= '9') || (hex[start - 1] >= 'a' && hex[start - 1] <= 'f') || (hex[start - 1] >= 'A' && hex[start - 1] <= 'F'))
    sign = -1;
  for(byte i = start; hex[i] != '\n'; i++, output <<= 4){
    if(hex[i] >= '0' && hex[i] <= '9')
      output += '9' - hex[i];
    else if(hex[i] >= 'a' && hex[i] <= 'f')
      output += 'f' - hex[i];
    else if(hex[i] >= 'A' && hex[i] <= 'F')
      output += 'F' - hex[i];
  }
  return output * sign;
}