- 论坛徽章:
- 0
|
标准c怎么实现数字到字符转换?
自己写一个吧,unix下好像是不支持数字转成字符串.
我这边刚好写了一个,你看能不能用吧
- 其中LINT代表字符数组
- 定义为 typedef unsigned long LINT[18];
- void convert_LINT_to_ch(LINT a, char* pa, unsigned radix)
- {
- char buf[20] = "\0";
- int i;
- for (i=0; i<18; i++)
- {
- char *p; /* pointer to traverse string */
- char *firstdig; /* pointer to first digit */
- char temp; /* temp char */
- unsigned digval; /* value of digit */
- p = buf;
- firstdig = p; /* save pointer to first digit */
- do {
- digval = (unsigned) (a[i] % radix);
- a[i] /= radix; /* get next digit */
- /* convert to ascii and store */
- if (digval >; 9)
- *p++ = (char ) (digval - 10 + 'a'); /* a letter */
- else
- *p++ = (char ) (digval + '0'); /* a digit */
- } while (a[i] >; 0);
- /* We now have the digit of the number in the buffer, but in reverse
- order. Thus we reverse them now. */
- *p-- = '\0'; /* terminate string; p points to last digit */
- do {
- temp = *p;
- *p = *firstdig;
- *firstdig = temp; /* swap *p and *firstdig */
- --p;
- ++firstdig; /* advance to next two digits */
- } while (firstdig < p); /* repeat until halfway */
- strcat(pa, buf);
- if(i<17)
- strcat(pa, ".");
- }
- }
- 转换之后存在char *pa中,对应数组的每一项由'.'隔开
复制代码 |
|