Is the binary data I am converting to short valid?

I am reading a binary log file generated by a piece of hardware.

I have data in byte [].

If I need to read two bytes to create a short one, I can do something like this:

short value = (short)(byte[1] << 8);
value += byte[2];

      

Now I know the value is correct for valid data.

How can I tell if the file has been corrupted and lets say the FF FF values ​​were at those two places in the byte array?

When I look at the resulting FF FF to short conversion value, I get -1.

Is this a normal value for FF FF? Or did the computer just hit some kind of short anchor and flip with invalid data?

For my purposes, all numbers of these theses will be positive. If FF FF is actually short -1, then I just need to confirm that all my results are fast.

Thanks,
Keith

By the way, I also read other data types. I will only show them here because. The read function is the main part of reading from byte []. All other data types use the basic Read () function.

    public byte Read()
    {
        //advance position and then return byte at position 

        byte returnValue;
        if (_CurrentPosition < _count - 1)
        {
            returnValue= _array[_offset + ++_CurrentPosition];
            return returnValue;
        }
        else
            throw new System.IO.EndOfStreamException
                   ("Cannot Read Array, at end of stream."); 
    }


    public float ReadFloat()
    {
        byte[] floatTemp = new byte[4];
        for (int i = 3; i >= 0; i--)
        {
            floatTemp[i] = Read();
        }

        float returnValue = System.BitConverter.ToSingle
            (floatTemp, 0);

        if (float.IsNaN(returnValue))
        {
            throw new Execption("Not a Number");    
        }
        return returnValue;
    }


    public short ReadInt16()
    {
        short returnValue = (short)(Read() << 8);
        returnValue += Read();
        return returnValue;
    }

    public int ReadInt32()
    {
        int returnValue = Read() << 24;
        returnValue += Read() << 16;
        returnValue += Read() << 8;
        returnValue += Read();
        return returnValue;
    }

      

+1


source to share


6 answers


0xffff

(all bits equal to 1) equals -1 for signed shorts, yes. Read on for Two Supplements for more details. You can switch to a larger data type, or (as suggested by Grzenio) just use an unsigned type.



+3


source


Well, you seem to have found BitConverter

for singles. Now let's see if we can use it for everything else ...



MemoryStream mem = new MemoryStream(_array);


float ReadFloat(Stream str)
{
   byte[] bytes = str.Read(out bytes, 0, 4);
   return BitConverter.ToSingle(bytes, 0)
}

public int ReadInt32(Stream str)
{
   byte[] bytes = str.Read(out bytes, 0, 4);
   return BitConverter.ToInt32(bytes, 0)
}

      

+2


source


Have you tried using ushort (unsigned short)?

+1


source


I think you are better off working with System.BitConverter .

In particular, the ToInt16 method is used for short messages .

You didn't mention this in your question, but you should also make sure you know what it means the hardware device is writing its data. From your examples, it looks like float has little end, but integers in big endian? I doubt the hardware device will mix endianess in binary output.

+1


source


Yes 0xFFFF equals -1 for a signed short message.

Also, why don't you use BinaryReader

class
?

+1


source


The FFFF value for "short" is -1, but the FFFF value for "unsigned short" is 65536.

In this case, you should make sure to use an unsigned short circuit if you are confident that all of your values ​​will be positive.

0


source







All Articles