我转换为 Short 的二进制数据有效吗?
我正在读取由一台设备生成的二进制日志文件。
我的数据在一个字节[]中。
如果我需要读取两个字节来创建一个短路,我可以这样做:
short value = (short)(byte[1] << 8);
value += byte[2];
现在我知道该值对于有效数据来说是正确的。
我如何知道文件是否混乱,假设值 FF FF 位于字节数组中的这两个位置?
当我查看将 FF FF 转换为短路的结果值时,我得到 -1。
这是 FF FF 的正常值吗?或者计算机是否只是遇到了某种短界并以无效数据翻转?
就我而言,所有这些数字都将是正数。 如果 FF FF 实际上是短-1,那么我只需要验证我的所有结果都是正值。
谢谢你,
Keith
顺便说一句,我也在阅读其他数字数据类型。 我会在这里向他们展示只是因为。 Read函数是读取byte[]的基本部分。 所有其他数据类型读取都使用基本的 Read() 函数。
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;
}
I am reading a binary log file produced by a piece of equipment.
I have the data in a byte[].
If I need to read two bytes to create a short I can do something like this:
short value = (short)(byte[1] << 8);
value += byte[2];
Now I know the value is the correct for valid data.
How would I know if the file was messed up and lets say the values FF FF were in those two places in the byte array?
When I look at the resultant value of converting FF FF to a short, I get a -1.
Is this a normal value for FF FF?, or did the computer just hit some kind of short bound and roll over with invalid data?
For my purposes all of theses numbers are going to be positive. If FF FF is actually a short -1, then I just need to validate that all my results are postitive.
Thank you,
Keith
BTW, I am also reading other number data types. I'll show them here just because.
The Read function is the basic part of reading from the byte[]. All the other data type reads 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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
对于有符号短裤,
0xffff
(所有位都等于 1)为 -1,是的。 请阅读二进制补码以了解更多详细信息。 您可以切换到更大的数据类型,或者(按照 Grzenio 的建议)仅使用无符号类型。0xffff
(all bits equal to 1) is -1 for signed shorts, yes. Read up on Two's complement to learn more about the details. You can switch to a larger datatype, or (as suggested by Grzenio) just use an unsigned type.好吧,您似乎已经找到了针对单身人士的
BitConverter
。 现在让我们看看我们是否可以将它用于其他所有事情......Well, you seemed to have found
BitConverter
for singles. Now let's see if we can get to to use it for everything else as well...您是否尝试过使用ushort(无符号短整型)?
Did you try to use ushort (unsigned short)?
我认为使用 System.BitConverter 会更好班级。
特别是对于 ToInt16 方法。
您在问题中没有提到它,但您还应该确保您知道硬件设备正在写入数据的字节顺序。从您的示例来看,浮点数采用小端字节序,但整数采用大端字节序? 我怀疑硬件设备会在其二进制数据输出中混合字节顺序。
I think you would be better served using the System.BitConverter class.
Specifically for shorts the ToInt16 method.
You didn't mention it in your question, but you should also make sure that you know what endianess the hardware device is writing its data in. From your examples it looks like the float is in little endian, but the integers are in big endian? I doubt a hardware device would mix endianess in its output of binary data.
是的,对于有符号短整型,0xFFFF 为 -1。
另外,为什么不使用
BinaryReader< /code> 类
?
Yes 0xFFFF is -1 for a signed short.
Also, why wouldn't you use the
BinaryReader
class?“short”的 FFFF 值为 -1,但“unsigned Short”的 FFFF 值为 65536。
在这种情况下,如果您确定所有值都将是,则应确保使用的是 unsigned Short。积极的。
The value of FFFF for a "short" is -1 but the value of FFFF for an "unsigned short" is 65536.
In this case you should make sure you are using an unsigned short if you are sure that all of your values will be positive.