作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
例如,我有一个类,它有short、byte、int类型的成员变量。
class A{
short a;
byte b;
int c;
}
如果我序列化或转换为字节数组,则该数组是意外值。
如果值类似于,
A a = new A();
a.a = 3;
a.b = 0x02;
a.c = 15;
然后,我期望它的字节为,
00 03 02 00 00 00 0F
那么...如何像这样序列化对象?
它需要我的套接字服务器...其他语言
最佳答案
如果你想要一个字节数组,你可以这样做。但是,如果您使用的是 DataOutputStream 之类的东西,最好只调用 writeInt、writeShort...
A a = new A();
a.a = 3;
a.b = 0x02;
a.c = 15;
ByteBuffer bb = ByteBuffer.allocate(7).order(ByteOrder.BIG_ENDIAN);
bb.putShort(a.a).put(a.b).putInt(a.c).flip();
byte[] buffer = bb.array();
for (byte b : buffer)
System.out.printf("%02X ", b);
关于java - 如何在java中使用紧凑字节将对象序列化为字节数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34763034/
我是一名优秀的程序员,十分优秀!