English 中文(简体)
原始数据
原标题:Parse Binary Data

我有一份申请,通过蓝色语接收二元数据。 我将数据从蓝色体字塔到沥青。 但是,我必须调和所有信息,因为它们必须有某种有效格式。 所有电文都是双向的。

My solution was to convert the byte[] to a string and then split the string and parse all received messages.

An example of the data to parse: 0000000010000001

我应该知道,头8点零是头脑,10000001是真实数据。

My idea was to create a string (from the byte[]) that represents -> 0000000010000001 using new String(byte[]) and then split the whole string in one byte and check the value, like:

string1 had 00000000 string2 had 10000001

我知道,8个零点是头盔,因此,有2个数据。

我的问题是这一方法的效率。 在流动环境中这样做的最佳方法吗?

最佳回答

在速度、记忆消耗方面,试图操纵双平线数据的做法相当低,也给垃圾收集者造成相当沉重的负担,因为你总是产生新的扼杀目标,并立即忘记这些物体。

在 Java,你们有好几项选择:

  • DataInputStream: is a wrapper for an InputStream where you read bytes, shorts, longs, doubles, etc directly from the stream.
  • NIO using ByteBuffers and "derived" types like ShortBuffer... these are good for bulkd data transfers and also for binary parsing.

只要你的数据按部就班,就很容易做到。 如果情况不如此,则会更好地了解如何使用<代码>和>、、、>>、<>~、><<、>>

我建议你遵守<条码>。 数据输入数据

问题回答

努力是做到这一点的最有效途径。

如果您的双程<代码>000010000001实际上是byte[]{0, 0, 0, 2, 0, 1} in decimal, 则仅对b[0] = b = b[3] = b[4]=0进行检查。

Edit: Ignore the "in decimal" part, it s irrelevant as long as you only want to check the if your array begins with 00000000 (i.e. 4 zero bytes)

It is a very low efficiency way, and is not recommended. To parse binary data, we should have the protocol, and always use bit operation. If you are not fimiliar with that,

FastProto是一个更好的选择,你需要做的是说明数据标的存档,Proto帮助你完成全部工作。

缩略语

65 00 7F 69 3D 84 7A 01 00 00 55 00 F1 FF 0D 00 00 00 07 00

双轨数据包含3种不同类型的信号,具体议定书如下:

Byte Offset Bit Offset Data Type(C/C++) Signal Name Unit Formula
0 unsigned char device id
2-9 long time ms
12-13 short temperature
import org.indunet.fastproto.annotation.*;

public class Weather {
    @UInt8Type(offset = 0)
    int id;

    @TimeType(offset = 2)
    Timestamp time;

    @Int16Type(offset = 12)
    int temperature;
}

byte[] bytes= ...   // binary need parsing

Weather weather = FastProto.parse(datagram, Weather.class);

也许你们已经注意到,“快车道”通过说明将实地信息描述为二元数据,说明非常简单、高效。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签