Re: Trying to understand some C/C++ code
bufferOverflow wrote:
Greetings all,
I am trying to understand a piece of C++ code that I am trying to port
to Java.
void Buffer::packInteger(int value){
int netval = htonl(value); // convert to network order (big-endian)
data = (char *) realloc(data, datalen + 4); //allocate datalen + 4
bytes to data
char* temp = data + datalen; //temp is allocated a pointer to (data
+datalen) ?
memcpy(temp, &netval, 4); // copy the first 4 bytes from netval to
temp;
datalen += 4; //increment datalen by 4
}
Now if I understood the comments in the code correctly, I can't seem
to understand how the integer called "value" is being packed even at
all in the code above!
It is converted to a network byte order integer netval. netval is "packed" using
memcpy to the end of the newly allocated storage area.
My stab of the code above in Java is something like this -
public void packInteger(int value) {
//b is defined as byte b[ ] = new byte[4];
b[0] = (byte) ((val >> 24) & 255);
b[1] = (byte) ((val >> 16) & 255);
b[2] = (byte) ((val >> 8) & 255);
b[3] = (byte) (val & 255);
}
AFAICT, the code above shifts 24,26,8 and 0 places to the right and
then ANDs it with 255 to get the values at those position
respectively. Is this how it's done ? Or am I missing some critical
pieces. I think I am. :-(
There should be no need to do this operation in Java. All integers are written
in network byte order, regardless of the hardware platform. It can be made into
a NOOP if you use an internal buffer and write the integer to it.
int netval = htonl(value); // convert to network order (big-endian)
not required, use ByteBuffer later to do it for us.
data = (char *) realloc(data, datalen + 4); //allocate datalen + 4
byte[] newData = new byte[data.length+4];
System.arraycopy(data,0,newData,0,data.length);
data = newData;
bytes to data
char* temp = data + datalen; //temp is allocated a pointer to (data
+datalen) ?
memcpy(temp, &netval, 4); // copy the first 4 bytes from netval to
temp;
Use a ByteBuffer to wrap the byte array, then we can use the byte ordered
methods to store the int in network byte order (the default ByteBuffer is
BIG_ENDIAN which is the same as network byte order).
ByteBuffer bb = ByteBuffer.wrap(data, datalen, 4);
bb.putInt(int);
datalen += 4; //increment datalen by 4
datalen = data.length;
--
Nigel Wade, System Administrator, Space Plasma Physics Group,
University of Leicester, Leicester, LE1 7RH, UK
E-mail : nmw@ion.le.ac.uk
Phone : +44 (0)116 2523548, Fax : +44 (0)116 2523555