Java equivalent of Python's struct.pack? Java equivalent of Python's struct.pack? python python

Java equivalent of Python's struct.pack?


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();


Something like this:

final ByteArrayOutputStream data = new ByteArrayOutputStream();final DataOutputStream stream = new DataOutputStream(data);stream.writeUTF(name);stream.writeUTF(password);final byte[] bytes = stream.toByteArray(); // there you go

Later, you can read that data:

final DataInputStream stream = new DataInputStream(  new ByteArrayInputStream(bytes));final String user = stream.readUTF();final String password = stream.readUTF();


I started development of project which is very close to Python Struct: java-binary-block-parserin JBBP it will look like

JBBPOut.BeginBin().Int(0,0).Short(21,96).Int(512).End().toByteArray();