[SOLVED] How can I convert String to long long?

Hello, I am working on an RFID project which involves reading big numbers and I am stuck on converting String variable "s_c_card_number" to long long variable. Please help me, any help would be appreciated. Code is in the link.

One method is to parse a long.
atol() is a C++ function that parses a long from a string.

I tried that, but there was some error. Anyways, thanks for a fast answer. When I mean big numbers, I mean, like 13 digit long numbers.

Edit: that's why long is not enough, I need long long.

Try to split the string in two and do it in pieces.
It's not a number until you make it a number.

But how will I connect it in long long later?

First, what Arduino are you using, please?

Currently, Uno, maybe I will move the project to Mega 2560 later.

Btw, I am in a time zone where it's nearly midnight, so I will come back tomorrow, anyways, thank you.

No, worries. Good night.
I will reply more in a bit, feel free to come back to it at your convivence tomorrow.

Ok, thanks.

strtoll (string to long long) or
strtoull (string to unsigned long long)

If you have a String object, you will need to convert to a null terminated c string.



1 Like

So you tried stoll, right? stoll - C++ Reference

Hi @leon_usmiani,

try this way:

As the number is very large, to print it it was necessary to separate it and use sprintf().
RV mineirin

RV mineirin

unsigned long long x ;
unsigned long long y;
unsigned long long z;
void setup()
  String number = "1234567890123";
  y = (number .substring(0, 8)).toInt();
  z = (number .substring(8)).toInt();
  x = (y * 100000) + z;

  char buffer[100];
  sprintf(buffer, "%0ld", x / 1000000L);
  sprintf(buffer, "%0ld", x % 1000000L);
void loop() {}
1 Like

@ruilviana Thank you for the most complete answer. There is one thing that confuses me, does the number of the zeros in <x = (y * 100000) + z;> matter or not?

EDIT: I am asking this because sometimes the number will have less than 13 digits and sometimes more. Anyways the code works like a charm for me. I don't actually need to print long long, I can use String, but I needed long long for storing card numbers in the "database" on SD card. I still have many work to do on this project.

It turns out that number of "zeros" really matters. I will try making some solution using sizeof() and then I will post it. It is funny how I actually asked myself a question and got an answer. :slight_smile:

A uno can’t print a long long directly. If you need to display the value, using the string you parsed is a possibility

strtoll would avoid all the mess

Sizeof won’t do you any good…. It is always the size of the storage (8 byte for a long long) regardless of the content

@J-M-L I just said that I don't need to print long long, and I can use String, but I need long long (integer type variable) for storing numbers, so no problems. :slight_smile:

EDIT: I need sizeof second substring to determine how many zeros I need.

Why don’t you use strtoll? (As suggested above a few times by other members)

String number = "1234567890123";
long long value = strtoll(number.c_str(), nullptr, 10);

If you have a String object use the length() method to know it’s size

Screenshot 2021-10-19 at 09.06.15
Thanks for reminding me of length(). :slight_smile:

That’s a good reason for not using it. :slight_smile: