Re: How do i convert a byte[2] with format big endian signed int to a integer value?

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



On Sep 30, 9:23 pm, Martin CLAVREUIL <m...@xxx> wrote:
Wouldn't something (simple) like that do the job ?

foreach (TwoBytes sample in sampledata)
{
short result = (short)(((short)sample.Byte2) |
(short)(sample.Byte1 * 256));
Console.WriteLine("{0}|{1} =
{2}",sample.Byte1.ToString(),sample.Byte2.ToString(),result.ToString());
}
where byte1 is the left (index=0) one.

Hope it helps
Great thank you!
public static int getIntegerFromBytes(byte[] word) {
if(word.Length != 2) {
throw new ArgumentOutOfRangeException("word should have length 2,
but has a length of " + word.Length);
}
return (((short)word[1]) | (short)(word[0] * 256));
}
Works very nice!

I also managed to get the library working (after looking to the source
code and reposting the question) When one wants to use the library,
the following code should be used (so others stumbling uponthis thread
will find an answer).
public static int getIntegerFromBytes(byte[] word) {
if(word.Length != 2) {
throw new ArgumentOutOfRangeException("word should have length 2,
but has a length of " + word.Length);
}
MiscUtil.Conversion.EndianBitConverter converter =
MiscUtil.Conversion.EndianBitConverter.Big;
return converter.ToInt16(word,0);
}


.