且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在C中将char转换为整数?

更新时间:2023-11-11 20:50:16

在过去,当我们可以假设大多数计算机使用 ASCII 时,我们会这样做

In the old days, when we could assume that most computers used ASCII, we would just do

int i = c[0] - '0';

但在 Unicode 时代,这不是一个好主意.如果您的代码必须在非 ASCII 计算机上运行,​​这绝不是一个好主意.

虽然它看起来很老套,但显然它由标准保证工作.谢谢@Earwicker.

Although it looks hackish, evidently it is guaranteed by the standard to work. Thanks @Earwicker.