gpt4 book ai didi

c# - 将整数转换为ASCII然后再返回整数后,为什么会得到一个不同的值?

转载 作者:行者123 更新时间:2023-12-03 15:59:01 26 4
gpt4 key购买 nike

为什么当我将INT值转换为字节,再转换为ASCII并返回时,又得到另一个值?
例子:

var asciiStr = new string(Encoding.ASCII.GetChars(BitConverter.GetBytes(2000)));
var intVal = BitConverter.ToInt32(Encoding.ASCII.GetBytes(asciiStr), 0);
Console.WriteLine(intVal);

// Result: 1855

最佳答案

ASCII仅是7位-不支持127以上的代码点。每个the docs on ? 会将不支持的字符转换为Encoding.ASCII:

The ASCIIEncoding object that is returned by this property might not have the appropriate behavior for your app. It uses replacement fallback to replace each string that it cannot encode and each byte that it cannot decode with a question mark ("?") character.


因此 2000十进制= D0 07 00 00十六进制(小端)= [unsupported character] [BEL character] [NUL character] [NUL character] = ? [BEL character] [NUL character] [NUL character] = 3F 07 00 00十六进制(小端)= 1855十进制。

关于c# - 将整数转换为ASCII然后再返回整数后,为什么会得到一个不同的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64263799/

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