Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

packing bytes into a fixed length array

807591Jun 15 2008 — edited Jun 16 2008
Hi,

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!


Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 14 2008
Added on Jun 15 2008
6 comments
1,648 views