【发布时间】:2017-11-20 18:52:06
【问题描述】:
得到以下错误:
System.OverflowException 已被抛出 - 值太大 或者对于无符号字节来说太小了。
有人知道如何解决吗?
class MainClass
{
public static void Main(string[] args)
{
int decValue = 2210;
string bin = Convert.ToString(decValue, 2);
string lowerbyte = bin.Substring(Math.Max(0, bin.Length - 16));
if (lowerbyte.Length < 16)
{
lowerbyte = lowerbyte.PadLeft(16, '0');
}
Int16 circular = Convert.ToByte(CicrularLeftShift(lowerbyte, 3), 2);
string xored = Convert.ToString((circular ^ 38556), 2).Substring(Math.Max(0, Convert.ToString((circular ^ 38556), 2).Length - 16));
//converting final binary shift value to HEX
string finalHex = Convert.ToString(Convert.ToInt32(xored, 2), 16).ToUpper();
Console.WriteLine(finalHex);
}
private static string CicrularLeftShift(string key, int shift)
{
return key.Substring(shift, key.Length - shift) + key.Substring(0, shift);
}
}
【问题讨论】:
-
当输入超出无符号字节的范围时,您希望发生什么?您将
Convert.ToByte的结果分配给Int16也有点奇怪——您想转换为short吗? -
不清楚你想得到什么,但我怀疑你需要将数字转换为字符串,然后再转换为数字,然后再转换为字符串来做到这一点。您的预期结果是什么?
-
你期待什么结果?
-
中断时的值是
"0100010100010000"(这是CircularLeftShift的返回值) - 所以:你想用它做什么?您正在尝试将 15 位(从第一个非零)转换为一个字节(8 位)。你的意思是Convert.ToInt16? -
作为旁注:通过字符串操作执行二进制让我对核心不寒而栗......
标签: c#