java 数据类型转字节数组
我有一个 Java 类,
public class MsgLayout{
int field1;
String field2;
long field3;
}
我必须将此对象作为字节数组写入 Socket 输出流中。这三个字段(实例变量)具有布局。即field1
必须占用1个字节,field2
必须占用4个字节,field3
必须占用8个字节。
ByteBuffer bbf = ByteBuffer.allocate(TOTAL_SIZE);
bbf.put(Integer.toString(this.getField1()).getBytes(), 0, FIELD1_SIZE);
bbf.position(FIELD2_OFFSET);
bbf.put(Long.toString(this.getField2()).getBytes(), 0, FIELD2_SIZE);
bbf.position(FIELD3_OFFSET);
bbf.put(Long.toString(this.getField3()).getBytes(), 0, FIELD3_SIZE);
byte[] msg = bbf.array();
使用上面的代码,我尝试根据所需的大小来适应字节数组中的每个字段。但我收到了 IndexOutOfBoundException
简而言之,问题是如何使字段适合布局定义的尺寸。例如FIELD1_OFFSET = 0、FIELD1_SIZE=1、FIELD2_OFFSET=1、FIELD2_SIZE=4、FIELD3_OFFSET=5、FIELD3_SIZE=8。
现在,当我将 field1
转换为 String 时,转换为 byte[] 时它不适合 1 个字节。如果我不转换为 String,并使用 putInt(int) ,它会将 4 个字节写入生成的字节数组中。
I have a Java class
public class MsgLayout{
int field1;
String field2;
long field3;
}
I have to write this object as a byte array in a Socket output stream. The three fields (instance variables) have a layout. i.e. field1
must occupy 1 byte, field2
must occupy 4 bytes and field3
must occupy 8 bytes.
ByteBuffer bbf = ByteBuffer.allocate(TOTAL_SIZE);
bbf.put(Integer.toString(this.getField1()).getBytes(), 0, FIELD1_SIZE);
bbf.position(FIELD2_OFFSET);
bbf.put(Long.toString(this.getField2()).getBytes(), 0, FIELD2_SIZE);
bbf.position(FIELD3_OFFSET);
bbf.put(Long.toString(this.getField3()).getBytes(), 0, FIELD3_SIZE);
byte[] msg = bbf.array();
Using the above code, I am trying to fit each field in the byte array according to its desired size. But I am getting IndexOutOfBoundException
In short, the problem is about how to fit the fields in the layout-defined size. For Example FIELD1_OFFSET = 0, FIELD1_SIZE=1, FIELD2_OFFSET=1, FIELD2_SIZE=4, FIELD3_OFFSET=5, FIELD3_SIZE=8.
Now when I convert field1
into String, it does not fit into 1 byte when converted into byte[]. If I do not convert to String, and use putInt(int) it writes 4 bytes into the resulting byte array.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的代码当前正在执行的操作是将数字字段编码为字符串,然后输出这些字符的字节。
我建议使用 DataOutputStream 类来包装您的 SocketOutput 流并按如下方式编写二进制数据:
这段代码中有几个假设。首先,我假设对于字段 2,您希望将 4 个字符分别序列化为单个字节。如果您想使用 UTF-8 之类的东西进行任何多字节编码,那么您需要做一些不同的事情。其次,我假设字段 2 始终至少有 4 个字符。
What your code is currently doing is encoding your numeric fields as strings and then outputting the bytes of those characters.
I would suggest using the DataOutputStream class to wrap your SocketOutput stream and write your binary data as so:
There are a couple assumptions in this code. First I'm assuming for field 2 you want 4 characters serialized as a single byte each. If you want to do any multibyte encoding using something like UTF-8, then you need to do something a little differently. Second, I'm assuming that field 2 will always have at least 4 characters.
field1
可能只有一个字节的数据,但其字符串表示形式将是一个或多个字符(例如"0"
、"63"
、“127”
)。字符串中的每个字符实际上都是一个char
(一个两字节值)。因此,我希望当一个字节的数据经过 byte->String->byte[] 转换时,它会膨胀到两到六个字节的数据。field1
may only have one byte of data, but its string representation will be one or more characters (e.g."0"
,"63"
,"127"
). Each character in the String is in fact achar
(a two byte value). So I would expect one byte of data to inflate to two to six bytes of data when it goes through a byte->String->byte[] conversion.