Re: How do i convert a byte[2] with format big endian signed int to a integer value?
- From: eywitteveen@xxxxxxxxx
- Date: Sun, 30 Sep 2007 13:11:55 -0700
On Sep 30, 9:23 pm, Martin CLAVREUIL <m...@xxx> wrote:
Wouldn't something (simple) like that do the job ?Great thank you!
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
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);
}
.
- References:
- How do i convert a byte[2] with format big endian signed int to a integer value?
- From: eywitteveen
- Re: How do i convert a byte[2] with format big endian signed int to a integer value?
- From: Martin CLAVREUIL
- How do i convert a byte[2] with format big endian signed int to a integer value?
- Prev by Date: Re: please ignore - test message
- Next by Date: Re: Preserve State
- Previous by thread: Re: How do i convert a byte[2] with format big endian signed int to a integer value?
- Next by thread: RTF Editor
- Index(es):