Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Adding Hexadecimal to byte array

 
amit punekar
Ranch Hand
Posts: 544
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello there,
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.

Regards,
Amit
 
Rob Spoor
Sheriff
Pie
Posts: 20668
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use 0x2 and 0x3 to get hexadecimal values. If you assign these to bytes / byte array elements directly if they are are between -128 (0xFFFFFFFFFFFFFF80) and 127 (7F).
 
amit punekar
Ranch Hand
Posts: 544
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rob,
Thanks and appreciate your reply.
You can use 0x2 and 0x3 to get hexadecimal values.


This is what I am having trouble understanding. I am not able to get how can I add these HEX values.
Below is the code snippet that I am using to write the message.



Thanks once again,
Amit
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15490
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Class DataOutputStream has a write method to write a byte. So it's very simple:

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.

 
amit punekar
Ranch Hand
Posts: 544
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Thank you Jesper for your reply.

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.

Thanks,
Amit
 
amit punekar
Ranch Hand
Posts: 544
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jesper,
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.

Thanks once again everyone for your replies.

Regards,
Amit
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15490
43
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic