Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert binary data to hexadecimal in a shell script

I want to convert binary data to hexadecimal, just that, no fancy formatting and all. hexdump seems too clever, and it "overformats" for me. I want to take x bytes from the /dev/random and pass them on as hexadecimal.

Preferably I'd like to use only standard Linux tools, so that I don't need to install it on every machine (there are many).

like image 345
davka Avatar asked Jun 09 '11 12:06

davka


People also ask

How use XXD command in Linux?

Use xxd as a filter within an editor such as vim(1) to hexdump a region marked between 'a' and 'z'. Use xxd as a filter within an editor such as vim(1) to recover a binary hexdump marked between 'a' and 'z'.

How to convert decimal to hexadecimal in a shell script?

Objective: Convert a number in decimal representation to hexadecimal format in a shell script. So, let’s say that we have a variable d that has a value of 255 (decimal). To convert the representation to hex, we can use one of the following methods. The first method is by using bc.

How to convert binary to hexadecimal in Linux?

For binary to hexadecimal use: xxd tool in linux and for binary to decimal you can use qalculate tool. For help regarding xxd type xxd --help or man xxd in Linux. Show activity on this post.

How to convert from decimal to Hex in Python?

To convert the representation to hex, we can use one of the following methods. The first method is by using bc. Note that the obase tells bc the base of the output number. The input base is by default 10 or decimal.

Which cmdlet is used to produce hexadecimal output?

Summary: Use the Format-Hex cmdlet to produce hexadecimal output. How can I use Windows PowerShell to put data in a binary key in the registry?


2 Answers

Perhaps use xxd:

% xxd -l 16 -p /dev/random 193f6c54814f0576bc27d51ab39081dc 
like image 51
unutbu Avatar answered Oct 14 '22 20:10

unutbu


Watch out!

hexdump and xxd give the results in a different endianness!

$ echo -n $'\x12\x34' | xxd -p 1234 $ echo -n $'\x12\x34' | hexdump -e '"%x"' 3412 

Simply explained. Big-endian vs. little-endian :D

like image 24
Zibri Avatar answered Oct 14 '22 22:10

Zibri