How to cast Hex-String to uint64_t ?

How can I cast a String of a hexnumber to an uint64_t ?

tobic:
How can I cast a String of a hexnumber to an uint64_t ?

You can't.
You need to convert the ASCII representation to binary, and this cannot be done with a cast.
There must be plenty of code available to do this, but a word of warning - use strings, not Strings.

Here a starter for the function you need, the commented line needs to be expanded
to include ABCDEF and optionally abcdef

``````uint64_t convertStringToU64(String str)   // char * preferred
{
char t[16] = "0123456789ABCDEF";

uint64_t val = 0;
for (int i = 0; i < str.length(); i++)
{
val = val * 16;
val = val + str[i] - '0';  // convert char to numeric value << this line needs to be expanded to include ABCDEF
}
return val;
}
``````

you can test this code by using a string like “314159265”

Check this method:

uint64_t stringToUint_64(String value) {
int stringLenght = value.length();

uint64_t uint64Value = 0x0;
for(int i = 0; i<=stringLenght-1; i++) {
char charValue = value.charAt(i);

uint64Value = 0x10 * uint64Value;
uint64Value += stringToHexInt(charValue);
}

return uint64Value;
}

int stringToHexInt(char value) {
switch(value) {
case ‘0’:
return 0;
break;
case ‘1’:
return 0x1;
break;
case ‘2’:
return 0x2;
break;
case ‘3’:
return 0x3;
break;
case ‘4’:
return 0x4;
break;
case ‘5’:
return 0x5;
break;
case ‘6’:
return 0x6;
break;
case ‘7’:
return 0x7;
break;
case ‘8’:
return 0x8;
break;
case ‘9’:
return 0x9;
break;
case ‘A’:
case ‘a’:
return 0xA;
break;
case ‘B’:
case ‘b’:
return 0xB;
break;
case ‘C’:
case ‘c’:
return 0xC;
break;
case ‘D’:
case ‘d’:
return 0xD;
break;
case ‘E’:
case ‘e’:
return 0xE;
break;
case ‘F’:
case ‘f’:
return 0xF;
break;
}
}

:o

``````uint64_t stringToBinary (String hexString)
{
return stringToBinary (hexString.c_str ());
}

uint64_t stringToBinary (const char* hexString)
{
int length = min (16, strlen (hexString));
uint64_t returnValue = 0;
for (int i = 0; i < length && isxdigit (hexString[i]); ++i) {
char digit = toupper (hexString [i]);
returnValue *= 16;
returnValue += (digit >= '0' && digit <= '9') ? digit - '0' : digit - 'A' + 10;
}
return returnValue;
}
``````

(compiled, but not fully tested)

You could try

``````uint64_t n;
char s[] = "F3B3D6";
sscanf(s, "%lx", &n);
``````