"Convert" String (HEX) to long long

Hi there,
first i have to admit that i'm no trained programmer at all. So, sorry if I cannot express the problem correctly.
I have a given String:

String TAG = "320F3550AE";

in the following code I need a long long with this String as input.
It works if i do it this way:

long long x = 0x320F3550AE;

So I'm looking for a way to transfer the String information to the long long in a way that it represents the same as in long long x = 0x320F3550AE;.
Can someone please tell me how to do it?
I tried it with "Stoll" and other ways but nothing works out.
Thanks in advance!
Wolf

char str[]="0x320F3550AE";
long n = strtoul(str,NULL,16);

Where does this String come from a and does it have to be a String or could it be a C style string ?

Thanks for the idea, but this seems not to work. The result is incorrect. I think 10 digits are too big for strtoul.

it comes from:

  while (ssrfid.available()) {
  char ssvalue = (char)ssrfid.read();
  if (isAlphaNumeric(ssvalue)) {
  StringRead+= ssvalue;

So StringRead is the String with the information i need. If i can read it directly to "long long" it would be great.

Which arduino are you using? Some board platforms do not support printing long long, or the long long conversion functions.

I use a nano.

https://cplusplus.com/reference/cstdlib/strtoull/

1 Like

Just NANO doesn't help because there are so many variants with different processors.

strtoull() does not appear to be implemented on the classic nano.

Is there any particular reason you want to convert the RFID code to a number?

Lets start with another question, why do you need the number in numeric form? This looks to be an RFID number.

@jremington : strtoull seems not to work. It runs in an error compiling it. It seems not to be available for arduino.

@oldcurmudgeon: It a Nano V3 Atmega328 CH340

Ok, thanks for all the help. Please let's not start to question the needs. I know where that leeds. But to your questions:
I need it, so i can convert the HEX to the number it represents to show it on a display.
I have it all working but for the connection beteen the String and the long long.

Since the number is in hexidecimal, you could separate the first two character and the remaining 8 characters, do the conversion separately, then re-combine with a bit of multiplication and addition.

Nano does not support printing long long, you will need to split it up a bit.

Do you want to print in base 10 or base 16?

I found a way to print it in the end:

long long x = 0x320F3550AE;
byte indexArray[12];
for(int i =0; i<12; i++)
{
    indexArray[i] = x%10;
  x = x / 10;
}

String blobb = "";
int f = sizeof(indexArray) -1;
while (f >= 0)
{
  blobb = blobb + indexArray[f];
  f--;
}
Serial.print("blobb: ");
Serial.println(blobb);

Works great in the way I need it
I assume the answer for the base is 10. On display it should be decimal.

You will want to make sure there are actually ten hex digits in the string for this to work correctly:

void setup() {
  Serial.begin(9600);
  
  char str[] = "320F3550AE";
  char s2[3] = {0};
  char s8[9] = {0};
  strncpy(s2, str, 2);
  strncpy(s8, str+2, 8);
  long n2 = strtol(s2, NULL, 16);
  long n8 = strtol(s8, NULL, 16);
  long long x = n2 * 0x100000000 + n8;

  byte indexArray[12];
  for (int i = 0; i < 12; i++)
  {
    indexArray[i] = x % 10;
    x = x / 10;
  }

  String blobb = "";
  int f = sizeof(indexArray) - 1;
  while (f >= 0)
  {
    blobb = blobb + indexArray[f];
    f--;
  }
  Serial.print("blobb: ");
  Serial.println(blobb);
}

void loop() {
}

Oops, major error. I used long instead of unsigned long.

void setup() {
  Serial.begin(9600);
  
  char str[] = "320F3550AE";
  char s2[3] = {0};
  char s8[9] = {0};
  strncpy(s2, str, 2);
  strncpy(s8, str+2, 8);
  unsigned long n2 = strtoul(s2, NULL, HEX);
  unsigned long n8 = strtoul(s8, NULL, HEX);
  unsigned long long x = n2 * 0x100000000 + n8;

  char indexArray[13] = {0};
  for (size_t i = 0; i < sizeof(indexArray) - 1; i++)
  {
    indexArray[sizeof(indexArray) - 2 - i] = '0' + x % 10;
    x = x / 10;
  }

  Serial.print("blobb: ");
  Serial.println(indexArray);
}

void loop() {
}

Personally, I’d get rid of the String. Just throwing away memory and cpu cycles fo no benefit.

sscanf() should help get your work done.

@david_2018:
After hours of despair your help finally gave me redemption!
it works as needed! Thank you very much!
Wolf