I'm trying to create an array of 16-bit values to send over a socket in network order.
First, I have defined an array to hold the value:
char[] txbuf = new char[32]; // Java "char" is 16-bits
Next, I open the socket and create the OutputStreamWriter:
InetAddress serverAddr = InetAddress.getByName(host);
socket = new Socket(serverAddr, port);
writer = new OutputStreamWriter(socket.getOutputStream(),Charset.forName("UTF-16BE"));
Then I initialize the data to send:
txbuf[0] = 0x1234;
...
txbuf[31] = 0xFFFF;
Finally, I send the data to the socket:
writer.write(txbuf,0,32);
write.flush();
However, the device I am sending the data to is behaving erratically, and I suspect there is some issue with the UTF-16BE conversion actually doing something to the data (filtering characters out?) besides the simple network order translation. Everything works fine in C, but not in the Java port.
I have seen various convoluted schemes for trying to write raw binary data but was trying for what seemed like the simplest and most straightforward method.
Is there a problem with the above scheme?