Java 相当于 Python 的 struct.pack? [英] Java equivalent of Python's struct.pack?

查看:31
本文介绍了Java 相当于 Python 的 struct.pack?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Java 中是否有任何与 Python 的 struct.pack 等效的函数允许我像这样打包和解包值?

Is there any function equivalent to Python's struct.pack in Java that allows me to pack and unpack values like this?

pump_on = struct.pack("IIHHI", 0, 0, 21, 96, 512)

推荐答案

我想你可能想要的是 ByteBuffer:

I think what you may be after is a ByteBuffer:

ByteBuffer pump_on_buf = ...
pump_on_buf.putInt(0);
pump_on_buf.putInt(0);
pump_on_buf.putShort(21);
pump_on_buf.putShort(96);
pump_on_buf.putInt(512);
byte[] pump_on = pump_on_buf.array();

这篇关于Java 相当于 Python 的 struct.pack?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆