gpt4 book ai didi

c# - 将 char 转换为 int 是 C# 中的安全操作吗?

转载 作者:太空狗 更新时间:2023-10-29 22:11:01 25 4
gpt4 key购买 nike

给定以下代码:

string source = "Some Unicode String";
foreach( char value in source ) {

int y = (int)value;

}

从 char 到 int 的转换是否有可能失败(以及在什么情况下)?

最佳答案

charint 的转换不会失败,任何 char 值。

来自 .NET 4.0 reference

The .NET Framework uses the Char structure to represent a Unicode character. The Unicode Standard identifies each Unicode character with a unique 21-bit scalar number called a code point, and defines the UTF-16 encoding form that specifies how a code point is encoded into a sequence of one or more 16-bit values. Each 16-bit value ranges from hexadecimal 0x0000 through 0xFFFF and is stored in a Char structure. The value of a Char object is its 16-bit numeric (ordinal) value.

关于c# - 将 char 转换为 int 是 C# 中的安全操作吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11158439/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com