Convert hex et decimal using array

I really do not understand why i have such values !
I need to convert an array of containing value into decimal value. I tried with a known array and the val calculated is not what it should be !
It does work under c console but not in IDE Arduino work

Thanks in advance

unsigned long hex2int(char *a, unsigned int len)
{   
   unsigned char ab[8] ={0, 0, 0, 0 ,0, 0, 1, 1}; // Test
   unsigned long val = 0;
   len = 8;
   Serial.println(val);
   Serial.println();
   for(int i=0;i<len;i++)                   
      if(*(ab+i) <= 57)                                   // If  0 <char< 9  (48<ascii<57)
       {
        val += ((*(ab+i)-48)<<(4*(len-1-i)));      //  translate to left 16*i times
        Serial.print(*(ab+i));                             // 16^1+ 16^2 + 16^3 .... according to i .
        Serial.print("     ");
        Serial.println(val);
       }
      else
       {
       val += (*(ab+i)-55)*(1<<(4*(len-1-i)));       // If  A <char<F   (65<ascii<70)
       Serial.print(*(ab+i));
       Serial.println("     ");
       Serial.println(val);  
       } 
}
}

![](http://<a target=)">

The compiler doesn't like italics.
Use code tags

Please read these two posts:

How to use this forum - please read.
and
Read this before posting a programming question ...
You may also find useful information that would answer your question here:
Useful links - check here for reference posts / tutorials

You have posted code without using code tags. The code tags make the code look

like this

when posting source code files. It makes it easier to read, and can be copied with a single mouse click. Also, if you don't do it, some of the character sequences in the code can be misinterpred by the forum code as italics or funny emoticons.
If you have already posted without using code tags, open your message and select "modify" from the pull down menu labelled, "More", at the lower left corner of the message. Highlight your code by selecting it (it turns blue), and then click on the "</>" icon at the upper left hand corner. Click on the "Save" button. Code tags can also be inserted manually in the forum text using the code and /code metatags.

Unless the sketch is too large, it's better if you post your code, rather than attach it. When it's attached, we have to download it, create a folder then open your code in our IDE. And afterwards, the folder remains unless we navigate to the "Temp" folder and manually remove it. It's much easier to just view the code in your post.

There are many other things that programmers do to make their code understandable. Please do them, as a courtesy to the members who volunteer their time to help you here. One is to give things descriptive names. You can name numerical constants, pin numbers, variables and many other things in this way. For example, you can refer to a pin and an output level by number, like

digitalWrite(3,0)

. But such a statement doesn't reveal anything about the purpose.

digitalWrite(hornRelayPin, LOW)

does. You can do that by declaring

const byte hornRelayPin = 3;

before setup() in your program.

Hint:

'0' == 48

I'm not sure what you're trying to do, but does this help? (I have no idea what len is, which is why you should post all of your code, not a snippet.)

void setup() {
  char ab[8] = {2, 1, 0, 0 , 3, 1, 1, 1}; // Test
  int len;

  Serial.begin(9600);
  hex2int(ab, sizeof(ab) / sizeof(ab[0]) );
}

void loop() {

}

unsigned long hex2int(char *a, unsigned int len)
{
  Serial.println();
  for (int i = 0; i < len; i++) {
    Serial.print("0x");
    Serial.print(a[i] < 16 ? "0" : "");
    Serial.println(a[i], HEX);
  }
}

Convert some hexadecimal data (ASCII) to binary.

char inHex[] = "0123456789ABCDEFDEADFEEDBEAFDAD1";
byte outData[(sizeof(inHex) - 1) / 2];

void setup() {
  Serial.begin(250000);
  unHex(inHex, outData, strlen(inHex));
  printBuffer();
}
void loop() {}

void unHex(const char* inP, byte* outP, size_t len) {
  for (; len > 1; len -= 2) {
    byte val = asc2byte(*inP++) << 4;
    *outP++ = val | asc2byte(*inP++);
  }
}

byte asc2byte(char chr) {
  byte rVal = 0;
  if (isdigit(chr)) {
    rVal = chr - '0';
  } else if (chr >= 'A' && chr <= 'F') {
    rVal = chr + 10 - 'A';
  }
  return rVal;
}

void printBuffer() {
  Serial.print(F("input '"));
  Serial.print(inHex);
  Serial.print(F("' output"));
  for (byte i = 0; i < sizeof(outData); i++) {
    Serial.write(' ');
    if (outData[i] < 16) {
      Serial.write('0');
    }
    Serial.print(outData[i], HEX);
  }
  Serial.println();
}
input '0123456789ABCDEFDEADFEEDBEAFDAD1' output 01 23 45 67 89 AB CD EF DE AD FE ED BE AF DA D1

Finaly, it works with different syntax, even if i do not understand why!

long b = 1 ;
long c = b << (4*(len-1-i)) ; WORK
val += (*(a+i)-48)*c;

//val += ((a+i)-48)(1<<(4*(len-1-i))); NOT WORK !!!
//val += ((a+i)-48)<<(4(len-1-i))); NOT WORK !!!

Just to convert the ID of a RFID Badge
=> In fact an arrray ( size = len) of ascci characteres read with SofwareSerial library

If anybody have any idea the reason why ?

unsigned long hex2int(long a, unsigned int len)
{
unsigned long val = 0;
Serial.write(" ");
for (int i=0; i<8; i++)
{
if(
(a+i) <= 57 )
{
long b = 1 ;
long c = b << (4*(len-1-i)) ; //val += ((a+i)-48)(1<<(4*(len-1-i))); NOT WORK !!!
val += ((a+i)-48)c; //val += ((a+i)-48)<<(4(len-1-i))); NOT WORK NEITHER !!!
}
else
{
long b = 1 ;
long c = b << (4*(len-1-i)) ;
val += (*(a+i)-55)*c;
}
}
Serial.println(val);
}

I already showed you how to convert a hex string of any length.

If your problem is merely reading a hex representation of an unsigned long int, use

char hexData[] = "DEADBEEF";
unsigned long value = strtoul(hexData, NULL, 16);
 if(*(a+i) <= 57 )

Are you deliberately obfuscating?

if( a[i] <= '9')