简体   繁体   English

将5个字节转换为整数

[英]Converting 5 bytes to an integer

I have some weird data that's stored in 5 bytes and I need to be able to convert it to an int to be able to manipulate it easier. 我有一些奇怪的数据存储在5个字节中,我需要能够将它转换为int,以便能够更容易地操作它。 I have some Python code (that was provided to me) that does this already, but I need to be able to have a solution in Java. 我有一些Python代码(提供给我)已经这样做了,但我需要能够有一个Java解决方案。 I think my problem is now Python and Java differ in the way they store bytes. 我认为我的问题是现在Python和Java存储字节的方式不同。

Python (98% sure this works correctly): Python(98%确定这可以正常工作):

def bin5(b,k):
  """ Returns binary integer from bytes k,k+1,...,k+4 in b."""
  b0 = b[k  ]
  b1 = b[k+1]
  b2 = b[k+2]
  b3 = b[k+3]
  b4 = b[k+4]
  if b0<0: b0 += 256
  if b1<0: b1 += 256
  if b2<0: b2 += 256
  if b3<0: b3 += 256
  if b4<0: b4 += 256
  return b0*65536.0+b1*256.0+b2+b3/256.0+b4/65536.0

Java attempt: Java尝试:

  // Returns binary integer from bytes k,k+1,...,k+4 in b.
  private static int bin5(byte[] b, int k) {
    byte b0 = b[k];
    byte b1 = b[k + 1];
    byte b2 = b[k + 2];
    byte b3 = b[k + 3];
    byte b4 = b[k + 4];
    return (int)(b0 * 65536.0 + b1 * 256.0 + b2 + b3 / 256.0 + b4 / 65536.0);
  }

I'm certain the problem is in the last return statement of the Java code. 我确定问题出在Java代码的最后一个return语句中。 Also, it will work for some byte arrays, but not for others. 此外,它适用于某些字节数组,但不适用于其他字节数组。 I can't find a reason for this behavior. 我找不到这种行为的原因。

EDIT: Example: If the Python code reads the bytes: 0 11 -72 0 0 for b0 thru b5 respectfully, it will change the -72 to 184 and then calculate the value 3000.0 based on the equation above. 编辑:示例:如果Python代码按b0到b5读取字节: 0 11 -72 0 0 ,它会将-72更改为184 ,然后根据上面的等式计算值3000.0 Based on the survey/data parameters, this value is correct. 根据调查/数据参数,此值是正确的。

My intuition says that the python code IS faulty for some values. 我的直觉说python代码对于某些值是错误的。 One such value is when it reads a values 0 -127 -66 0 0 (b0 thru b5 respectfully) which turns into: 0 129 190 0 0 and then the value 33214 is output by the conversion. 一个这样的值是当它读取值0 -127 -66 0 0 (b0至b5)时变为: 0 129 190 0 0然后通过转换输出值33214 This is impossible based on the survey/data parameters. 根据调查/数据参数,这是不可能的。 BUT there is a possibility that this could be a faulty data point. 但这可能是一个错误的数据点。

EDIT 2: 0 13 9 0 0 should return 3337 (and does in the python code). 编辑2: 0 13 9 0 0应该返回3337(并在python代码中)。 However under Java, it returns 3593. 但是在Java下,它返回3593。

You could do 你可以做到

private static double bin5(byte[] b, int k) {
    int b0 = b[k] & 0xFF;     // treat as unsigned byte
    int b1 = b[k + 1] & 0xFF;
    int b2 = b[k + 2] & 0xFF;
    int b3 = b[k + 3] & 0xFF;
    int b4 = b[k + 4] & 0xFF;
    return (b0 * 65536 + b1 * 256 + b2 + b3 / 256.0 + b4 / 65536.0);
}

As powers of 2 can be represented exactly with double you won't get any rounding error. 由于2的幂可以用double精确表示,因此不会出现任何舍入误差。

This code is strange; 这段代码很奇怪; it does not return an integer at all but a float... 它根本不会返回一个整数,而是浮点数...

Anyway, the Java equivalent of the python code is something like this (note: NO bounds checking is done at all ): 无论如何,相当于Java的Python代码的是这样的(注:没有边界检查是在全部完成):

private static double bin5(final byte[] b, final int k)
{
    final ByteBuffer buf = ByteBuffer.allocate(8);
    buf.position(3);
    buf.put(b, k, 5);
    buf.rewind();
    final long l = buf.getLong();
    return (double) l / 65536.0;
}

EDIT: if the last two elements of the byte array at offset k are always 0 (which it looks like they are) then you can replace from buf.rewind() onwards with: 编辑:如果偏移量k处的字节数组的最后两个元素总是0(它看起来像是),那么你可以从buf.rewind()开始替换为:

buf.position(2);
return (double) buf.getInt();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM