I have a code that returns array of bytes which could be one byte or more or less. The problem relies in that when I convert the bytes some times I get an error because I can't determine when I need to use toInt32, toInt64, or toInt16. The other problem is that sometimes when I get one byte I am not able to convert this byte using these above methods because I keep getting errors. So how to determine based maybe on the length or size of bytes which method I should use.
//as in array of bytes byte[]
var response = this.cc.Sendcc("SERIAL_NUMBER", 0x05, 0x80, 0x64, 0x04, 0x01, 0x21, (byte)1, (byte)1);
if (response .Length == 1)
{
toInt32SerialNumber = BitConverter.ToInt16(response , 0);
}
else
{
toInt32SerialNumber = BitConverter.ToInt32(response , 1);
}
@moe1792
Please try this code,To convert bytes array to an int using bit converter in c#:
I hope above code will be useful for you.
Thank you.