添加字符时获取数字.为什么



我试图连接字符串的字符,但却得到了数值。为什么会这样?

C#

using System;
namespace std {
class Program {
static void Main(string[] args) {
string n = "helloWorld!";
Console.WriteLine(n[0] + n[1] + n[2] + n[3] + n[4] + n[5]);
}
} 
}

C++

#include <iostream>
using namespace std;
int main() {
string x = "helloWorld!";
cout << x[0] + x[1] + x[2] + x[3] + x[4] + x[5] << endl;
return 0;
}

输出(两种语言(-

619

预期输出-

helloW

所有x[i]都是字符。Char存储一个数值,该数值表示一个字符,具体取决于您使用的编码。当您执行x[i] + x[j]时,您不是将它们连接起来,而是简单地添加它们的数值。

最新更新