Search

Sponsored Links

Meta

Categories

Archives

Recent Posts

RSS Feeds

22
Mar

Unsigned long long (uint64) to string (ulltostr)

Related Blog Items

Here is the code snippet which does conversion from unsigned long long (uint64) to
string/ascii/char *. 

it takes input as unsigned long long (uint64), converts it to string, it takes care of the base also.

example usage of function ulltostr is: ulltostr(1023234343453553, ptr, 10);

here is the code snippet….

 

 
#include <stdio.h>
 
#ifdef _MSC_VER
typedef unsigned __int64 uint64;
#else
typedef unsigned long long uint64
#endif
 
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);
}

please post your comments and suggestion if any….

Popularity: 13%

You need to log on to convert this article into PDF


Related Blog Items

No Comments

No comments yet.

Leave a comment

*
To prove you're a person (not a spam script), type the security word shown in the picture.
Anti-spam image