Unsigned long long to string convert

I am trying to convert unsigned long long to a string like this

unsigned long long Data = 12;
char Str[20];

sprintf(Str, "%lld",Data);

when i want to see but i always see 00

Str[0],Str[1]....;

what's wrong!!!

+3
source share
3 answers

In most cases, %llushould do the trick. But you may have to use it %I64uon some Windows platforms.

+4
source

%lldfor signed long long, use instead %llu.

+3
source

% llu should work as it has been added to the standard. however, you can use the secure version of snprintf or consider writing your own function better than snprintf. You may be interested here.

char *ulltostr(uint64 value, char *ptr, int base)
{
  uint64 t = 0, res = 0;
  uint64 tmp = value;
  int count = 0;

  if (NULL == ptr)
  {
    return NULL;
  }

  if (tmp == 0)
  {
    count++;
  }

  while(tmp > 0)
  {
    tmp = tmp/base;
    count++;
  }

  ptr += count;

  *ptr = '\0';

  do
  {
    res = value - base * (t = value / base);
    if (res < 10)
    {
      * --ptr = '0' + res;
    }
    else if ((res >= 10) && (res < 16))
    {
        * -- ptr = 'A' - 10 + res;
    }
  } while ((value = t) != 0);

  return(ptr);
}
+1
source

Source: https://habr.com/ru/post/1770083/


All Articles