Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Output an array of bytes in hex

Tags:

ruby

hex

sockets

I have a stream of bytes in my Ruby-script and I'd like to output the values to the console.

like image 533
scrrr Avatar asked Aug 01 '11 09:08

scrrr


People also ask

How do you convert a byte array to a hexadecimal string?

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.

How do I print a byte array?

You can simply iterate the byte array and print the byte using System. out. println() method.

How many hex is 4 bytes?

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.

How do you write a byte in hexadecimal?

A byte (or octet) is 8 bits so is always represented by 2 Hex characters in the range 00 to FF.


1 Answers

If you read your stream in chunks of bytes, then you could use String#unpack:

while buffer = io.read
  str << buffer.unpack('H*')
end
like image 88
emboss Avatar answered Nov 02 '22 14:11

emboss