Arduino: Converting uint64_t to string
Asked Answered
R

6

5

I have a binary that I was able to convert to a uint64_t. It's big, so I really needed a uint64_t. I'm having trouble converting it to a char array. I can do it in a standalone project but not on Arduino

Some roadblocks that I encountered:

  • I can't use sprintf ("%llu"): It's giving me a result of 0 and further googling shows that it wasn't really implemented
  • I can't use itoa: Yes, itoa was working for smaller numbers, but i'm dealing with a uint64_t and it seems like it reached its limit and giving me a negative result
  • I can't use String(123456789): I can use it for other types like int and long, but I can't pass in a uint64_t because it's not supported in the parameters
  • I can't use long long: Searching for it only gives me a variation on uint64_t (eg. use sprintf)
  • I'm having trouble using VC include in Visual Studio: When i go to my Project Properties > Configuration Properties > C/C++ > General > Additional Include Drectories and add in the path "C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\" Visual Studio deletes it.

Any input is greatly appreciated.

Rustie answered 10/12, 2014 at 2:11 Comment(0)
R
1

To those who are having the same problem: I did a workaround where I just went through the digits one by one and making them a char. This helped me: How can I iterate through each digit in a 3 digit number in C?

Rustie answered 10/12, 2014 at 3:56 Comment(2)
Hey, can you elaborate on how you did this? I tried following the other post you linked to, but it was not of much help to me. Thanks!Spook
Please always add complete answers instead of links, as the community guidelines suggest. Thanks.Ownership
O
6

Assuming you want to print "number" in HEX:

  uint64_t number;
  unsigned long long1 = (unsigned long)((number & 0xFFFF0000) >> 16 );
  unsigned long long2 = (unsigned long)((number & 0x0000FFFF));

  String hex = String(long1, HEX) + String(long2, HEX); // six octets
Outskirts answered 16/6, 2017 at 16:27 Comment(1)
I think they want decimal, since that's what "%llu" returns.Decorous
M
6

This is like the sequel to the answer by @john.

I use the following function:

String uint64ToString(uint64_t input) {
  String result = "";
  uint8_t base = 10;

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

    if (c < 10)
      c +='0';
    else
      c += 'A' - 10;
    result = c + result;
  } while (input);
  return result;
}

Source: https://github.com/markszabo/IRremoteESP8266/blob/master/src/IRutils.cpp#L66

Mirk answered 8/4, 2018 at 15:11 Comment(0)
N
6

Just to add onto the list of possible solutions. I use the following function:

char *uint64_to_string(uint64_t input)
{
    static char result[21] = "";
    // Clear result from any leftover digits from previous function call.
    memset(&result[0], 0, sizeof(result));
    // temp is used as a temporary result storage to prevent sprintf bugs.
    char temp[21] = "";
    char c;
    uint8_t base = 10;

    while (input) 
    {
        int num = input % base;
        input /= base;
        c = '0' + num;

        sprintf(temp, "%c%s", c, result);
        strcpy(result, temp);
    } 
    return result;
}
Noe answered 10/5, 2018 at 18:50 Comment(0)
R
1

To those who are having the same problem: I did a workaround where I just went through the digits one by one and making them a char. This helped me: How can I iterate through each digit in a 3 digit number in C?

Rustie answered 10/12, 2014 at 3:56 Comment(2)
Hey, can you elaborate on how you did this? I tried following the other post you linked to, but it was not of much help to me. Thanks!Spook
Please always add complete answers instead of links, as the community guidelines suggest. Thanks.Ownership
S
1

I've created a simple library called Int64String for this purpose. It is very fast (from version 1.1.0), and supports any "base" from 2 to 16. It can be used anywhere that accepts a String type:

// LL and ULL suffixes allow the compiler to use the right type
Serial.println(int64String((int64_t)-9223372036854775808LL)); // cast required here for some reason
Serial.println(int64String(4527492349271ULL, 16));

Would output:

-9223372036854775808
41E2392BD57

It can be found in the Library Manager or at https://github.com/djGrrr/Int64String

Sutler answered 18/9, 2018 at 15:26 Comment(0)
B
0

any number, no matter how long it is, is stored with a bunch of bytes near each other. so effectively you want to convert an array of bytes into hex string representation of them.

char* numberToHexStr(char* out, unsigned char* in, size_t length)
{
        char* ptr = out;
        for (int i = length-1; i >= 0 ; i--)
            ptr += sprintf(ptr, "%02X", in[i]);
        return ptr;
}

usage

unsigned long long superlong = 0xF1F2F3F4F5F6F7F8; // 8 bytes
char str[32];
numberToHexStr(str, (unsigned char*) &superlong, sizeof(superlong));
printf("superlong = %s", str);
// outputs "superlong = F1F2F3F4F5F6F7F8"

NOTE this only works for positive numbers

Bundesrat answered 26/8, 2022 at 12:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.