请解释这段代码在做什么(someChar - 48) [英] Please explain what this code is doing (someChar - 48)

查看:17
本文介绍了请解释这段代码在做什么(someChar - 48)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在经历一些练习题,我看到了这段代码:

I'm going through some practice problems, and I saw this code:

#include <stdio.h>
#include <string.h>

int main(void) {
   char* s = "357";
   int sum = 0;
   int i = 0;
   for (i = 0; i < strlen(s); i++) {
     sum += s[i] - 48;
   }
   printf("Sum is %d", sum);

   return 0;
}

谁能解释一下代码的作用,尤其是 48 部分的减法?

Can someone explain what the code does, especially the subtraction with 48 part?

推荐答案

代码基本上是对表示为字符串的数字的数字求和.它做了两个重要的假设才能正常工作:

The code basically sums the digits of a number represented as a string. It makes two important assumptions to work properly:

  • 字符串仅包含 '0'..'9' 范围内的字符
  • 使用的字符编码是 ASCII

在 ASCII 中,'0' == 48'1' == 49 等等.因此,'0' - 48 == 0'1' - 48 == 1 等等.也就是说,减去 48 会将 char'0'..'9' 转换为 int0..9.

In ASCII, '0' == 48, '1' == 49, and so on. Thus, '0' - 48 == 0, '1' - 48 == 1, and so on. That is, subtracting by 48 translates the char values '0'..'9' to the int values 0..9.

因此,正是因为 '0' == 48,该代码也适用于:

Thus, precisely because '0' == 48, the code will also work with:

sum += s[i] - '0';

这个版本的意图可能更清楚一些.

The intention is perhaps slightly more clear in this version.

您当然可以通过添加来进行反向"映射,例如5 + '0' == '5'.同样,如果您有一个 char 包含 'A'..'Z' 范围内的字母,则可以减去" 'A'从中获取该字母在 0..25 范围内的索引.

You can of course do the "reverse" mapping by addition, e.g. 5 + '0' == '5'. Similarly, if you have a char containing a letter in 'A'..'Z' range, you can "subtract" 'A' from it to get the index of that letter in the 0..25 range.

  • 如何将单个char转换为int
  • 语言对决:将数字字符串转换为数组整数?
    • 这种数字转换的许多示例,使用 '0'48 的减法!
    • How to convert a single char into an int
    • Language showdown: Convert string of digits to array of integers?
      • Many examples of this digit conversion, using subtraction with both '0' and 48!

      如前所述,原始- 48 代码假定使用的字符编码是ASCII.- '0' 不仅提高了可读性,而且放弃了 ASCII 假设,并且可以与 any 编码一起使用,正如 C 语言规定的那样,它规定数字字符必须是在一个连续的块中按顺序编码.

      As mentioned, the original - 48 code assumes that the character encoding used is ASCII. - '0' not only improves readability, but also waives the ASCII assumption, and will work with any encoding, as specified by the C language which stipulates that digit characters must be encoded sequentially in a contiguous block.

      另一方面,对字母没有做出这样的规定.因此,在您使用 EBCDIC 编码的极少数情况下,例如,将 'A'..'Z' 映射到 0..25 不再那么简单减去 'A',因为字母 NOT 在 EBCDIC 的连续块中按顺序编码.

      On the other hand, no such stipulation is made about letters. Thus, in the rare situation where you're using EBCDIC encoding, for example, mapping 'A'..'Z' to 0..25 is no longer as simple as subtracting 'A', due to the fact that letters are NOT encoded sequentially in a contiguous block in EBCDIC.

      一些编程语言通过强制使用一种特定的编码来表示源代码来简化问题(例如,Java 使用 Unicode:JLS §3.1)

      Some programming languages simplify matters by mandating one particular encoding is used to represent the source code (e.g. Java uses Unicode: JLS §3.1)

      这篇关于请解释这段代码在做什么(someChar - 48)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆