2008-12-30 23:45:08 -05:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include "dtypes.h"
|
2010-08-28 01:07:02 -04:00
|
|
|
#include "utils.h"
|
2008-12-30 23:45:08 -05:00
|
|
|
|
2009-03-13 18:26:44 -04:00
|
|
|
char *uint2str(char *dest, size_t len, uint64_t num, uint32_t base)
|
2008-12-30 23:45:08 -05:00
|
|
|
{
|
2009-03-13 18:26:44 -04:00
|
|
|
int i = len-1;
|
|
|
|
uint64_t b = (uint64_t)base;
|
2008-12-30 23:45:08 -05:00
|
|
|
char ch;
|
|
|
|
dest[i--] = '\0';
|
|
|
|
while (i >= 0) {
|
|
|
|
ch = (char)(num % b);
|
|
|
|
if (ch < 10)
|
|
|
|
ch += '0';
|
|
|
|
else
|
|
|
|
ch = ch-10+'a';
|
|
|
|
dest[i--] = ch;
|
|
|
|
num /= b;
|
|
|
|
if (num == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return &dest[i+1];
|
|
|
|
}
|
2009-02-01 00:41:43 -05:00
|
|
|
|
|
|
|
int isdigit_base(char c, int base)
|
|
|
|
{
|
|
|
|
if (base < 11)
|
|
|
|
return (c >= '0' && c < '0'+base);
|
|
|
|
return ((c >= '0' && c <= '9') ||
|
|
|
|
(c >= 'a' && c < 'a'+base-10) ||
|
|
|
|
(c >= 'A' && c < 'A'+base-10));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* assumes valid base, returns 1 on error, 0 if OK */
|
2009-03-24 22:28:21 -04:00
|
|
|
/*
|
2009-02-01 00:41:43 -05:00
|
|
|
int str2int(char *str, size_t len, int64_t *res, uint32_t base)
|
|
|
|
{
|
|
|
|
int64_t result, place;
|
|
|
|
char digit;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
place = 1; result = 0;
|
|
|
|
for(i=len-1; i>=0; i--) {
|
|
|
|
digit = str[i];
|
|
|
|
if (!isdigit_base(digit, base))
|
|
|
|
return 1;
|
|
|
|
if (digit <= '9')
|
|
|
|
digit -= '0';
|
|
|
|
else if (digit >= 'a')
|
|
|
|
digit = digit-'a'+10;
|
|
|
|
else if (digit >= 'A')
|
|
|
|
digit = digit-'A'+10;
|
|
|
|
result += digit * place;
|
|
|
|
place *= base;
|
|
|
|
}
|
|
|
|
*res = result;
|
|
|
|
return 0;
|
|
|
|
}
|
2009-03-24 22:28:21 -04:00
|
|
|
*/
|