Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert string to hexadecimal on command line

I'm trying to convert "Hello" to 48 65 6c 6c 6f in hexadecimal as efficiently as possible using the command line.

I've tried looking at printf and google, but I can't get anywhere.

Any help greatly appreciated.

Many thanks in advance,

like image 880
Eamorr Avatar asked Jul 22 '11 14:07

Eamorr


People also ask

How do you convert text to hexadecimal?

The most common way to convert ASCII text to hexadecimal numbers manually is to first look up the decimal number for the letter in the ASCII table. Then, convert this decimal value to its hexadecimal equivalent. Also you might find a conversion table that directly converts ASCII to hexadecimal.

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 do you convert hex to ASCII in Linux terminal?

The command option -r converts the Hex characters to ASCII and command option -p prints the outcome in plain text. The final echo command produces a newline on the Linux terminal. If you wish to convert the Hex characters to ASCII characters from a file, use the cat command and pipe the file to xxd command.

How do you hex a string in Python?

Python hex() function is used to convert an integer to a lowercase hexadecimal string prefixed with “0x”. We can also pass an object to hex() function, in that case the object must have __index__() function defined that returns integer. The input integer argument can be in any base such as binary, octal etc.


1 Answers

echo -n "Hello" | od -A n -t x1 

Explanation:

  • The echo program will provide the string to the next command.
  • The -n flag tells echo to not generate a new line at the end of the "Hello".
  • The od program is the "octal dump" program. (We will be providing a flag to tell it to dump it in hexadecimal instead of octal.)
  • The -A n flag is short for --address-radix=n, with n being short for "none". Without this part, the command would output an ugly numerical address prefix on the left side. This is useful for large dumps, but for a short string it is unnecessary.
  • The -t x1 flag is short for --format=x1, with the x being short for "hexadecimal" and the 1 meaning 1 byte.
like image 164
Tomas Avatar answered Sep 26 '22 02:09

Tomas