I need to write this in Java.
Although I know how to read a file, I’m interested in what the ‘buff’ output is, is the length at the beginning?
char *buff;
unsigned char *aux;
while(fgets (buff+2, length, fin)){
len = strlen (buff + 2) + 2;
aux = (unsigned char *) &len;
buff[1] = aux[0];
buff[0] = aux[1];
...
send (sd, buff, len, 0);
}
but I don’t understand this:
aux = (unsigned char *) &len;
buff[1] = aux[0];
buff[0] = aux[1];
Thanks in advance.
This code:
creates a byte-array view of the integer value stored in
len. In Java, you can’t do exactly the same thing, but you can get what you want a couple of ways. The easiest is just with bit masking and shifting:Another possibility is to use a
ByteBuffer, but that seems overkill for such a simple operation.