This week's book giveaway is in the Mac OS forum. We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line! See this thread for details.
I am writing a byte array onto the output stream of the client socket.
While doing that I want to add delimiters as Hex "02" and Hex "03" at the start and end of my message. I tried using different methods but not able to figure out how I can do the same?
Appreciate your help in this regards.
By the way, thew writeBytes method of class DataOutputStream (which it inherits from interface DataOutput) is a really bad method. According to the documentation, it writes one byte for every character in the string. It ignores character encodings. You're going to get issues with this method if you write anything else than ASCII characters.
It's better to use String.getBytes(...), specifying the character encoding that you want the string in; UTF-8 is usually a good choice. Or use the writeUTF method, which properly delimits the string so that it can be read easily at the other side.
Yeah, my problem was I did not know how to represent or write HEX character (0x02) using Java method.
I will try the suggested and let you know.
Joined: May 14, 2004
Thank you for adding more information around the DataOutputStream method. Yeah, we are using this as a simulator and passing only ASCII characters. Though I will keep in mind your suggestion.
Moreover I did not know we can write the HEX characters using write(byte) method. Can you point me to some good documentation that can help me firm up my understanding around this. I was never good or comfortable with these different formats( HEX, OCTAL). I was really surprised to see how easy it was to write HEX on the stream.
It doesn't have anything to do with hexadecimal, octal or any other way to represent numbers - it's just writing bytes with the specified values. Note that these would have written exactly the same bytes:
Decimal, hexadecimal and octal are just different ways to express numbers. But the value of the numbers stays the same, no matter what notation you use for the number.