I have a stream of bytes in my Ruby-script and I'd like to output the values to the console.
To convert byte array to a hex value, we loop through each byte in the array and use String 's format() . We use %02X to print two places ( 02 ) of Hexadecimal ( X ) value and store it in the string st . This is a relatively slower process for large byte array conversion.
You can simply iterate the byte array and print the byte using System. out. println() method.
Byte to Hexadecimal. The bytes are 8 bit signed integers in Java. Therefore, we need to convert each 4-bit segment to hex separately and concatenate them. Consequently, we'll get two hexadecimal characters after conversion.
A byte (or octet) is 8 bits so is always represented by 2 Hex characters in the range 00 to FF.
If you read your stream in chunks of bytes, then you could use String#unpack:
while buffer = io.read
str << buffer.unpack('H*')
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With