免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: Law
打印 上一主题 下一主题

[C] 标准c怎么实现数字到字符转换? [复制链接]

论坛徽章:
0
1 [报告]
发表于 2003-05-02 16:25 |显示全部楼层

标准c怎么实现数字到字符转换?

自己写一个吧,unix下好像是不支持数字转成字符串.
我这边刚好写了一个,你看能不能用吧

  1. 其中LINT代表字符数组
  2. 定义为  typedef unsigned long LINT[18];
  3. void convert_LINT_to_ch(LINT a, char* pa, unsigned radix)
  4. {
  5.   char  buf[20] = "\0";
  6.   int i;
  7.   for (i=0; i<18; i++)
  8.   {
  9.     char *p;                /* pointer to traverse string */
  10.     char *firstdig;         /* pointer to first digit */
  11.     char temp;              /* temp char */
  12.     unsigned digval;        /* value of digit */

  13.     p = buf;
  14.     firstdig = p;           /* save pointer to first digit */

  15.     do {
  16.       digval = (unsigned) (a[i] % radix);
  17.       a[i] /= radix;       /* get next digit */

  18.       /* convert to ascii and store */
  19.       if (digval >; 9)
  20.         *p++ = (char ) (digval - 10 + 'a');  /* a letter */
  21.       else
  22.         *p++ = (char ) (digval + '0');       /* a digit */
  23.     } while (a[i] >; 0);

  24.     /* We now have the digit of the number in the buffer, but in reverse
  25.     order.  Thus we reverse them now. */

  26.     *p-- = '\0';            /* terminate string; p points to last digit */

  27.     do {
  28.       temp = *p;
  29.       *p = *firstdig;
  30.       *firstdig = temp;   /* swap *p and *firstdig */
  31.       --p;
  32.       ++firstdig;         /* advance to next two digits */
  33.     } while (firstdig < p); /* repeat until halfway */

  34.     strcat(pa, buf);
  35.     if(i<17)
  36.       strcat(pa, ".");
  37.   }
  38. }
  39. 转换之后存在char *pa中,对应数组的每一项由'.'隔开
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP