Very long hexadecimal String to decimal string, 10 characters!

Hello,

I have a question. I want to convert an 10 character long hexadecimal string into an decimal string. I've searched the intire internet and nothing to be found. It either works with only 4 character long hex strings or uses strol, which I can't seem to get working.

My code for 4 digit hex part:

// start of hex to dec decoding
    unsigned int hexToDec(String countrycode);
    unsigned int countrydecValue = 0;
    int nextInt;
    for (int i = 0; i < countrycode.length(); i++) {
      nextInt = int(countrycode.charAt(i));
      if (nextInt >= 48 && nextInt <= 57) nextInt = map(nextInt, 48, 57, 0, 9);
      if (nextInt >= 65 && nextInt <= 70) nextInt = map(nextInt, 65, 70, 10, 15);
      if (nextInt >= 97 && nextInt <= 102) nextInt = map(nextInt, 97, 102, 10, 15);
      nextInt = constrain(nextInt, 0, 15);
      countrydecValue = (countrydecValue * 16) + nextInt;
    }
    // end of hex to dec decoding

Now I have a 10 digit/charachter long one:
which it will do for 8/10 of the way with replacing the int to long:

long hexToDec1(String animalnumberfinal);
    long animaldecValue = 0;
    int nextInt3;
    for (int i = 0; i < animalnumberfinal.length(); i++) {
      nextInt3 = int(animalnumberfinal.charAt(i));
      if (nextInt3 >= 48 && nextInt3 <= 57) nextInt3 = map(nextInt3, 48, 57, 0, 9);
      if (nextInt3 >= 65 && nextInt3 <= 70) nextInt3 = map(nextInt3, 65, 70, 10, 15);
      if (nextInt3 >= 97 && nextInt3 <= 102) nextInt3 = map(nextInt3, 97, 102, 10, 15);
      nextInt3 = constrain(nextInt3, 0, 15);
      animaldecValue = (animaldecValue * 16) + nextInt3;  
    }

Yet it can't convert the whole string which is a must because I can't use it in piece's!

Sincerely,

Johannes

In a long (signed 4 bytes) you have up to 8 hex digits. You cannot store 10 hex digits in a long! You could use a long long (signed 8 bytes) or an unsigned long long.

Do you want something like this:

char myData[ ] = "7FAB1234AC"

The above line is to transformed nto the following:

char myCode[ ] = "548330943660";

GolamMostafa:
Do you want something like this:

char myData[ ] = "7FAB1234AC"

The above line is to transformed nto the following:
char myCode[ ] = "548330943660";

yes!

and btw, the underneath I tried but it still didnt work. Gave me a 10 digit output again and not the right one sadly.

ToddL1962:
In a long (signed 4 bytes) you have up to 8 hex digits. You cannot store 10 hex digits in a long! You could use a long long (signed 8 bytes) or an unsigned long long.

johanneshet:
yes!

I think that you need to apply manual approach:

1. Input/Output Data
(1) Assume that the input data is:

char myData[ ] = "7FAB1234AC";

(2) Desired output data is:

char myCode[ ] = "548330943660";
==> char myCode[] = {0x35, 0x34, 0x38, 0x33, 0x33, 0x30, 0x39, 0x34, 0x33, 0x36, 0x36, 0x30, 0x00};

2. Extract 07 0F 0A 0B 01 02 03 04 0A 0C from myData[] array of Step-1(1) and save them in array named myHex[].

for (int i = 0; i < sizeof(myData) - 1; i++)
  {
    x = myData[i];  //0x37
    myHex[i] = getHex(x);  //. see Step-6 for codes
    // Serial.print(myHex[i], HEX);//07 0F 0A 0B 01 02 03 04 0A 0C ; 7FAB1234AC
  }

3. Merge bytes of Step-2 to get binary/hex: 7FAB1234AC

p1 = myHex[0] << 4 | myHex[1]; //Serial.println(p1, HEX); //7F
  p2 =  myHex[2] << 4 | myHex[3]; //Serial.println(p2, HEX); //AB
  p3 = myHex[4] << 4 | myHex[5]; //Serial.println(p3, HEX);//12
  p4 = myHex[6] << 4 | myHex[7]; //Serial.println(p4, HEX);//34
  p5 = myHex[8] << 4 | myHex[9]; //Serial.println(p5, HEX);//AC
  y = (long long)p1 << 32 | (long long)p2 << 24 | (long long)p3 << 16 |
      (long long)p4 << 8 | (long long)p5;

4. Extract the individual digits of the decimal number of the hex/binary number of Step-3 by using % and / operators. Save the corresponding ASCII codes in the array named myCode[].

j = 0;
  do
  {
    k1 = y % 10;
    myCode[j] = k1+0x30;  //ASCII code
    y = y / 10;
    j++;
  }
  while (y != 0);

5. Check that the array myCode[ contains the ASCII codes of the digits of the desired decimal number. Take care of the endianness.

for (int k = j - 1; k >= 0; k--)
  {
    Serial.print(myCode[k], HEX); //35 34 38 33 33 30 39 34 33 36 36 30
  }

6. Complete skecth

char myData[] = "7FAB1234AC";//here f and F are different
char myCode[20];  //char myCode[ ] = "548330943660";
byte myHex[20];
byte x, k1;
long long y;
int j;
int p1, p2, p3, p4, p5;

//union  //for debugging purposes
//{
//  long long z;
//  long myByte[2];
//} data;

void setup()
{
  Serial.begin(9600);
  for (int i = 0; i < sizeof(myData) - 1; i++)
  {
    x = myData[i];  //0x37
    myHex[i] = getHex(x);
    // Serial.print(myHex[i], HEX);//07 0F 0A 0B 01 02 03 04 0A 0C ; 7FAB1234AC
  }
  //Serial.println();
  //-----------------------
  p1 = myHex[0] << 4 | myHex[1]; //Serial.println(p1, HEX); //7F
  p2 =  myHex[2] << 4 | myHex[3]; //Serial.println(p2, HEX); //AB
  p3 = myHex[4] << 4 | myHex[5]; //Serial.println(p3, HEX);//12
  p4 = myHex[6] << 4 | myHex[7]; //Serial.println(p4, HEX);//34
  p5 = myHex[8] << 4 | myHex[9]; //Serial.println(p5, HEX);//AC
  y = (long long)p1 << 32 | (long long)p2 << 24 | (long long)p3 << 16 |
      (long long)p4 << 8 | (long long)p5;
  //------------------------
  //data.z = y;
  //Serial.println(data.myByte[0], HEX);//AB1234AC
 // Serial.println(data.myByte[1], HEX); //0000007F

  j = 0;
  do
  {
    k1 = y % 10;
    myCode[j] = k1+0x30;  //ASCII code
    y = y / 10;
    j++;
  }
  while (y != 0);
  //-----------------
  //Serial.println();

  for (int k = j - 1; k >= 0; k--)
  {
    Serial.print(myCode[k], HEX); //35 34 38 33 33 30 39 34 33 36 36 30
  }
  
}

void loop()
{

}

byte getHex(byte t) //0x37
{
  if (t <= 0x39)
  {
    return (t & 0x0F); //07
  }
  else
  {
    return (t - 0x37);    //A = 0x41 - 0x37 = A
  }
}

7. Test result on Serial Monitor
smHexToDec.png

smHexToDec.png