Converting Byte array into integer?
-
Hello all, Looks like this is a basic question that I somehow is not able to figure it out properly. The method I am using works for Hex numbers that does not contain 0 in its lower byte. I guess I have to use a different approach to get around this problem, which I am not able to quite figure it out. I am reading values off of an IC which pushes out serial data in bits and the total length of the output is always 16bits which are read into two bytes. So, when the value is 255 its fine when I convert it into integer. But when the output from the chip is equivalent to 256, it turns out to be '1' in Upper byte and '0' in lower byte which is not being converted to 256 but rather to 16 using the following code.
string hexValue = In[0].ToString("X") + In[1].ToString("X");
int hscandacvalue = int.Parse(hexValue, System.Globalization.NumberStyles.HexNumber);Any suggestions? thanks
PKNT
-
Hello all, Looks like this is a basic question that I somehow is not able to figure it out properly. The method I am using works for Hex numbers that does not contain 0 in its lower byte. I guess I have to use a different approach to get around this problem, which I am not able to quite figure it out. I am reading values off of an IC which pushes out serial data in bits and the total length of the output is always 16bits which are read into two bytes. So, when the value is 255 its fine when I convert it into integer. But when the output from the chip is equivalent to 256, it turns out to be '1' in Upper byte and '0' in lower byte which is not being converted to 256 but rather to 16 using the following code.
string hexValue = In[0].ToString("X") + In[1].ToString("X");
int hscandacvalue = int.Parse(hexValue, System.Globalization.NumberStyles.HexNumber);Any suggestions? thanks
PKNT
The problem with this is that it turns eg 0x01 (as a byte) into the string 1, and then the leading zero is lost. That's a problem, because you're concatenating in front of it. You could fix that by using X2, but don't. But you don't need any of this weird string business anyway, you can simply use math (
lowbyte + 256 * highbyte
) -
The problem with this is that it turns eg 0x01 (as a byte) into the string 1, and then the leading zero is lost. That's a problem, because you're concatenating in front of it. You could fix that by using X2, but don't. But you don't need any of this weird string business anyway, you can simply use math (
lowbyte + 256 * highbyte
)Thanks Harold, that works great.
PKNT