Long long number to String

Hello

in my code I have a 12 digit long long number. It is named orignum
it value can be between 100000000000 and 700000000000

What I need to do is convert this number to a 12 character string called numStr that shows the same number.

I tried to use String numStr =String(orignum) but that does not work. It will work with long numbers but not long long numbers.

I am too far in to change the number type from long long

I tried to make it work using ltoa but it is not giving the right string output.

If anybody can point me in the right direction I would be grateful. I have added my sample code below. I am running this on an ESP32
Thank you
Chazza


long long orignum = 123456789012;

String numStr;

char buf[12];


void setup() {
 
  Serial.begin(115200);
  Serial.println();

  Serial.print("Original Number = ");
  Serial.println(orignum); Serial.println();


  ltoa(orignum, buf, 10);

  Serial.print("char buffer Number = ");
  Serial.println(buf); Serial.println();

  Serial.print("by char* Number = ");
  numStr = (char*)buf;
  Serial.println(numStr);  Serial.println();
  

  Serial.println("character buffer Number by Number = ");
  numStr = "";

  for (int i = 0; i < 12; i++) {
    Serial.print(i);
    Serial.print(". = ");
    Serial.println(buf[i]);
    numStr = numStr + char(buf[i]);
  }

  Serial.println();

  Serial.print("Number String = ");
  Serial.println(numStr);
}

void loop() {
}

Try lltoa(), which is meant for long long integers. There is no good reason to use Strings.

Hi
Thank you for the prompt response. It seems that that lltoa() is not available in the Arduino IDE
Regards
Chazza

1 Like

You can probably find the source code somewhere, or implement it yourself by division.

Arduino does not support printing long long, either. There are other threads on that topic in this forum. One is quite recent, with a solution.

Just convert your number with own code.

Position to the last char of the conversion buffer,
get the llNum % 10, add 0x30, put it in the buffer, decrement buffer index,
llNum /= 10, repeat while llNum != 0.

long long llNum = 123456789012;
char buffer[13] = "............";

void setup() {
  int pos;
  Serial.begin(115200);
  for (pos = sizeof(buffer) - 2; llNum; llNum /= 10) {
    buffer[pos--] = (llNum % 10) + '0';
    if (pos < 0) {
      break;
    }
  }
  Serial.println(buffer + ++pos);
}
void loop() {}
123456789012

or slightly simpler

long long llNum = 123456789012;
char buffer[13] = "............";

void setup() {
  int pos = sizeof(buffer) - 1;
  Serial.begin(115200);
  for (; llNum; llNum /= 10) {
    buffer[--pos] = (llNum % 10) + '0';
    if (pos <= 0) {
      break;
    }
  }
  Serial.println(buffer + pos);
}
void loop() {}

I've used this before...

void intToChar (uint64_t number, char string[])
{
  uint8_t digit = 0;
  uint8_t count = 0;

  for (uint64_t x = 100000000000000; x >= 1; x /= 10)
  {
    digit = number / x;
    string[count++] = 0x30 + digit;
    number = number - (digit * x);
  }
  string[count] = 0x00;
}

Or, if you want the result in a String:

long long orignum = 123456789012;

void setup()
{
  Serial.begin(115200);

  String number = lltoString(orignum);

  Serial.println(number);
}

String lltoString(long long ll)
{
  String result = "";
  do {
    result = int(ll % 10) + result;
    ll /= 10;
  } while (ll != 0);
  return result;
}

void loop() {}

Hi
Thank you for your help everybody. I tried all day yesterday to find a solution. Your support is very much appreciated. Every day is a school day as they say!
Chazza

Hi,
Thank you for this neat solution. I have decided to use this in my coding
Regards
Chazza

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.