问题
Without resorting to standard library utoa, I'm looking for source code of utoa so I may customise it for specific project. I have unsigned integer (32 bits) with output such as 0xFFFF_FFFF
I also looking for source code for unsigned integer and half word to string in binary format.
回答1:
Try this:
char *dec(unsigned x, char *s)
{
*--s = 0;
if (!x) *--s = '0';
for (; x; x/=10) *--s = '0'+x%10;
return s;
}
You call it with a pointer to the end of a caller-provided buffer, and the function returns the pointer to the beginning of the string. The buffer should have length at least 3*sizeof(int)+1
to be safe.
Of course this is easily adapted to other bases.
回答2:
Theres a lot of itoa source files easily found on google... That should give you what you want, eg. itoa from opensource.apple.com
Or write it from scratch, it's not too hard.
回答3:
That's not terribly hard. Keep dividing by 10 and use the remainder mod 10 as an index into "0123455679". You build this up from right to left, so you have to buffer the result and return it in reverse:
char * utoa(unsigned int n)
{
char * res, buf[30]; // long enough for largest number
unsigned int i, counter = 0;
if (n == 0)
buf[counter++] = '0';
for ( ; n; n /= 10)
buf[counter++] = "0123456789"[n%10];
res = malloc(counter);
for (i = 0; i < counter; ++i)
res[i] = buf[counter - i - 1];
return res;
}
回答4:
#include <stdint.h>
#include <string.h>
char * utox(uint32_t n) {
static char hexstr[sizeof(n)*2+1];
char * p = hexstr + sizeof(hexstr) -1;
int x;
memset(hexstr, '0', sizeof(hexstr));
*p-- = '\0';
while (n) {
x = n % 16;
if (x < 10)
*p-- = '0' + x;
else
*p-- = 'A' + x - 10;
n /= 16;
}
return hexstr;
}
This should do it, it zero pads. Simply changing the type of n
in the function parameters will make it work for any integer type/size.
回答5:
Most of the functions mentioned/suggested here uses modulus % operator, which is very expensive for embedded system.
So using divide by 10 idea is the only prominent option I guess. Here it is:
/*for byte which is 3 digit most*/
void itoa(unsigned char value,char *desitination)
{
desitination[0] = '\0';
desitination[1] = '\0';
desitination[2] = '\0';
desitination[3] = '\0';//you at least 4 char array, last char is NULL
while (value >= 100)
{
desitination[0]++;
value -= 100;
}
desitination[1] = '0';
while (value >= 10)
{
desitination[1]++;
value -= 10;
}
value+= '0';
desitination[2] =value;
}
回答6:
There are many implementation of itoa. This is one of them which takes under consideration base from 2 to 36:
/**
* C++ version 0.4 char* style "itoa":
* Written by Lukás Chmela
* Released under GPLv3.
*/
char* itoa(int value, char* result, int base) {
// check that the base if valid
if (base < 2 || base > 36) { *result = '\0'; return result; }
char* ptr = result, *ptr1 = result, tmp_char;
int tmp_value;
do {
tmp_value = value;
value /= base;
*ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz" [35 + (tmp_value - value * base)];
} while ( value );
// Apply negative sign
if (tmp_value < 0) *ptr++ = '-';
*ptr-- = '\0';
while(ptr1 < ptr) {
tmp_char = *ptr;
*ptr--= *ptr1;
*ptr1++ = tmp_char;
}
return result;
}
More variations can be found here which includes speed performance of the various implementations.
来源:https://stackoverflow.com/questions/6920554/source-code-in-embedded-c-for-unsigned-integer-to-string