packing bytes into a fixed length array
807591Jun 15 2008 — edited Jun 16 2008Hi,
I need to be sending a set of strings (converted to byte[] arrays and sent into the BytBuffer) across the network to a C program that will receive these as character strings of a fixed length (this length for now is agreed upon between the sending Java program and the receiving C program).
Now, I am having problems packing the bytes of an arbitrary string into this ByteBuffr and sending it across.
Here's my code snippet - please let me know what can I do to fix this?
byte[] tmpBytes = new byte[32];
Arrays.fill(tmpBytes, 0, tmpBytes.length, (byte)0);
tmpBytes = "Some String".getBytes();
pktBytesBuffer.put(tmpBytes, 0, tmpBytes.length);
Now, when the C program reads this, it reads the next field too since the 32 characters of this byte array are
not totally filled up by the value of "Some String".
What is a more efefctive way of achieving what I want to do?
Thanks!