如何将ASCII字符串转换为C中字符的十进制值?

时间:2016-01-18 20:29:23

标签: c

例如:

  • “16”应打印出小数值:49,54
  • “24”应打印出小数值:50,52

我如何实现这一目标?

1 个答案:

答案 0 :(得分:6)

这很简单,你不需要转换任何代表问题,例如

const char *sixteen = "16";
const char *twentyfour = "24";
const char *number = "1345461";

printf("%d,%d\n", sixteen[0], sixteen[1]);
printf("%d,%d\n", twentyfour[0], twentyfour[1]);
//       ^  ^ use the `%d' specifier to see the decimal value
//            of the corresponding ascii.
for (int i = 0 ; number[i] != '\0' ; ++i)
    printf("%d,", number[i]);