# Converting long long / uint64\_t to string or char

**URL:** <https://forum.arduino.cc/t/converting-long-long-uint64-t-to-string-or-char/1206906>\
**Category:** Programming\
**Created:** [January 4, 2024, 10:33am UTC](https://forum.arduino.cc/t/converting-long-long-uint64-t-to-string-or-char/1206906 "2024-01-04T10:33:49Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![trap-fish](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/trap-fish/32/951282_2.png) [@trap-fish](https://forum.arduino.cc/u/trap-fish)\
**Post date:** [January 4, 2024, 10:33am UTC](https://forum.arduino.cc/t/converting-long-long-uint64-t-to-string-or-char/1206906/1 "2024-01-04T10:33:49Z")

</div>

Struggling to convert this and the only other question I can find was [this fairly unhelpful one](https://forum.arduino.cc/t/convert-long-long-or-uint64-to-string/1053377/12)

I get the unix time (in s) but because I take many samples per second I convert this to ms then add millis() to it. This works fine as far as getting a unique timestamp goes, but it needs to be converted into a string for MQTT to handle.

I have tried some of the solutions in the linked thread such as this

```arduino
void publishJson() {
  char buffer[15];
  measurement_time.toCharArray(buffer, 13);

  String jsonString = "{";
  jsonString += "\"measurement_time\":" + String(buffer) + ",";
  jsonString += "}";
  Serial.println(jsonString);
}

```

and get the complie error -  
`error: request for member 'toCharArray' in 'measurement_time', which is of non-class type 'uint64_t {aka long long unsigned int}' measurement_time.toCharArray(buffer, 13);`

my original error is:

```arduino
Compilation error: call of overloaded 'String(uint64_t)' is ambiguous

```

---

<div class="post-metadata">

**Author:** ![david\_2018](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/david_2018/32/1013044_2.png) [@david\_2018](https://forum.arduino.cc/u/david_2018)\
**Post date:** [January 4, 2024, 12:11pm UTC](https://forum.arduino.cc/t/converting-long-long-uint64-t-to-string-or-char/1206906/2 "2024-01-04T12:11:51Z")

</div>

Try using sprintf to see if unsigned long long is supported.

```arduino
void setup(){
  char buffer[21]; //maximum value for uint64_t is 20 digits
  sprintf(buffer, "%llu", number);
```

---

<div class="post-metadata">

**Author:** ![Deva\_Rishi](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/deva_rishi/32/227254_2.png) [@Deva\_Rishi](https://forum.arduino.cc/u/Deva_Rishi)\
**Post date:** [January 4, 2024, 12:17pm UTC](https://forum.arduino.cc/t/converting-long-long-uint64-t-to-string-or-char/1206906/3 "2024-01-04T12:17:29Z")

</div>

You could just do it manually

```arduino
uint64_t val = measurement_time;
char buffer[15];
uint8_t ndx = 15;
while (ndx) {
  ndx--;
  uint8_t v = val % 10;
  val = val / 10; 
  buffer[ndx] = v + '0';
}

```

---

<div class="post-metadata">

**Author:** ![david\_2018](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/david_2018/32/1013044_2.png) [@david\_2018](https://forum.arduino.cc/u/david_2018)\
**Post date:** [January 4, 2024, 12:35pm UTC](https://forum.arduino.cc/t/converting-long-long-uint64-t-to-string-or-char/1206906/4 "2024-01-04T12:35:20Z")

</div>

> [@Deva\_Rishi](#):
>
> You could just do it manually

Be careful to include the terminating null at the end of the buffer, variables declared inside a function will not be automatically initialized to all zeros.

---

<div class="post-metadata">

**Author:** ![Deva\_Rishi](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/deva_rishi/32/227254_2.png) [@Deva\_Rishi](https://forum.arduino.cc/u/Deva_Rishi)\
**Post date:** [January 4, 2024, 12:39pm UTC](https://forum.arduino.cc/t/converting-long-long-uint64-t-to-string-or-char/1206906/5 "2024-01-04T12:39:38Z")

</div>

Ah sorry, good point ! , so that would be

```arduino
uint64_t val = measurement_time;
char buffer[15];
uint8_t ndx = 14;
buffer[ndx] = '\0';
while (ndx) {
  ndx--;
  uint8_t v = val % 10;
  val = val / 10; 
  buffer[ndx] = v + '0';
}

```

and afterwards

```arduino
if (val) ERROR

```

---

<div class="post-metadata">

**Author:** ![david\_2018](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/david_2018/32/1013044_2.png) [@david\_2018](https://forum.arduino.cc/u/david_2018)\
**Post date:** [January 4, 2024, 12:45pm UTC](https://forum.arduino.cc/t/converting-long-long-uint64-t-to-string-or-char/1206906/6 "2024-01-04T12:45:00Z")

</div>

Tried sprintf on a Mega and an UNO R4 WiFi, does not support unsigned long long on either.

---

<div class="post-metadata">

**Author:** ![david\_2018](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/david_2018/32/1013044_2.png) [@david\_2018](https://forum.arduino.cc/u/david_2018)\
**Post date:** [January 4, 2024, 1:01pm UTC](https://forum.arduino.cc/t/converting-long-long-uint64-t-to-string-or-char/1206906/7 "2024-01-04T13:01:37Z")

</div>

Need to be careful of the buffer size, and allow for the number of digits being less than the full buffer. Safer to use a buffer that will fix the maximum size of a uint64\_t.

```arduino
  uint64_t val = measurement_time;
  char buffer[21];
  char* ndx = &buffer[sizeof(buffer) - 1];
  *ndx = '\0';
  do {
    *--ndx = val % 10 + '0';
    val = val / 10;
  } while (val != 0);
  Serial.println(ndx);

```

This is a reduced form of the code used by Print for uint32\_t

```arduino
size_t Print::printNumber(unsigned long n, uint8_t base)
{
  char buf[8 * sizeof(long) + 1]; // Assumes 8-bit chars plus zero byte.
  char *str = &buf[sizeof(buf) - 1]; 

  *str = '\0';

  // prevent crash if called with base == 1
  if (base < 2) base = 10; 

  do {
    char c = n % base;
    n /= base;

    *--str = c < 10 ? c + '0' : c + 'A' - 10; 
  } while(n);

  return write(str);
}

```

---

<div class="post-metadata">

**Author:** ![gfvalvo](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gfvalvo/32/1013044_2.png) [@gfvalvo](https://forum.arduino.cc/u/gfvalvo)\
**Post date:** [January 4, 2024, 1:34pm UTC](https://forum.arduino.cc/t/converting-long-long-uint64-t-to-string-or-char/1206906/8 "2024-01-04T13:34:15Z")

</div>

> [@david\_2018](#):
>
> Tried sprintf on a Mega and an UNO R4 WiFi, does not support unsigned long long on either.

Works on an ESP32:

```arduino
void setup() {
  Serial.begin(115200);
  delay(2000);

  uint64_t x = 17179869184;
  Serial.printf("x = %llu\n", x);
}

void loop() {
}

```

```arduino
x = 17179869184

```

---

<div class="post-metadata">

**Author:** ![gfvalvo](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gfvalvo/32/1013044_2.png) [@gfvalvo](https://forum.arduino.cc/u/gfvalvo)\
**Post date:** [January 5, 2024, 1:59pm UTC](https://forum.arduino.cc/t/converting-long-long-uint64-t-to-string-or-char/1206906/11 "2024-01-05T13:59:40Z")

</div>

@trap-fish, if the code in [Post #8](https://forum.arduino.cc/t/converting-long-long-uint64-t-to-string-or-char/1206906/8) doesn't work on your platform, then you can use one of the roll-your-own solutions presented above to get a character string decimal representation.

There's also a more efficient option if you're OK with time stamps presented as a character string HEX representation:

```arduino
void setup() {
  Serial.begin(115200);
  delay(2000);

  uint64_t x = 4330422113570437784;
  uint32_t lower = x & 0xFFFFFFFF;
  uint32_t upper = (x >> 32) & 0xFFFFFFFF;
  Serial.printf("x = 0x%08X%08X\n", upper, lower);
}

void loop() {
}

```

```arduino
x = 0x3C18BFF7FEDCBA98

```

However, I think I've seen some posts that say AVR-based platforms have a problem shifting 64-bit integers. If you run across that, then you could go with:

```arduino
void setup() {
  Serial.begin(115200);
  delay(2000);
  uint32_t lower;
  uint32_t upper;

  uint64_t x = 4330422113570437784;
  uint8_t *ptr = reinterpret_cast<uint8_t *>(&x);
  memcpy(&upper, ptr + sizeof(uint32_t), sizeof(uint32_t));
  memcpy(&lower, ptr, sizeof(uint32_t));
  Serial.printf("x = 0x%08X%08X\n", upper, lower);
}

void loop() {
}

```

```arduino
x = 0x3C18BFF7FEDCBA98

```

As I mentioned above, either of the HEX solutions are more efficient than trying to get a decimal representation.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [July 3, 2024, 2:00pm UTC](https://forum.arduino.cc/t/converting-long-long-uint64-t-to-string-or-char/1206906/12 "2024-07-03T14:00:36Z")

</div>

Closed on July 3, 2024, 2:00pm UTC

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