Reputation: 85
Getting the following error :
System.OverflowException has been thrown - Value was either too large or too small for an unsigned byte.
Does anyone have any idea how to fix it?
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);
}
}
Upvotes: 6
Views: 21856
Reputation: 1423
You are trying to Convert.ToByte.
A Byte is 8 bits. You are supplying a 16 bit number in your string.
Change your references to 16, to 8 if you want a single Byte. Or if you do want Int16 then use Convert.ToInt16.
https://msdn.microsoft.com/en-us/library/4wxkt09k(v=vs.110).aspx
Upvotes: 5