C#:将二进制字符串转换为int,然后转换为字节,然后转换为二进制字符串。为什么结果不同?

时间:2019-03-06 10:47:39

标签: c# binary type-conversion

我正在努力找出这里发生了什么。这是我的示例代码:

byte testByte = 0;
string testString = "10011110";

int temp = Int32.Parse(testString);
Console.WriteLine(temp); // 10011110

testByte = (byte) temp;
Console.WriteLine(testByte); // 230, why not 158 and how to get it 158?

string result = Convert.ToString(testByte, 2);
Console.WriteLine(result); // 11100110 and not the original 10011110

以下是上面代码的小提琴:https://dotnetfiddle.net/pZUkUO

网络中的二进制/十进制转换器将原始10011110转换为158,在toString(testByte,2)转换后,原始则变为原始10011110。为什么Int32.Parse会将“ 10011110”转换为230?我应该如何转换以使结果与开始时相同?我也希望获得一些参考资料,以便我可以学习和了解更多有关此的内容。谢谢!

0 个答案:

没有答案
相关问题