wood burning stoves 2.0*
The moose likes Java in General and the fly likes Adding Hexadecimal to byte array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Adding Hexadecimal to byte array" Watch "Adding Hexadecimal to byte array" New topic
Author

Adding Hexadecimal to byte array

amit punekar
Ranch Hand

Joined: May 14, 2004
Posts: 516
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

Joined: Oct 27, 2005
Posts: 19790
    
  20

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).


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
amit punekar
Ranch Hand

Joined: May 14, 2004
Posts: 516
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

Joined: Aug 16, 2005
Posts: 14435
    
  23

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
amit punekar
Ranch Hand

Joined: May 14, 2004
Posts: 516
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

Joined: May 14, 2004
Posts: 516
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

Joined: Aug 16, 2005
Posts: 14435
    
  23

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Adding Hexadecimal to byte array