Linux hex to string

Conversion hex string into ascii in bash command line

I have a lot of this kind of string and I want to find a command to convert it in ascii, I tried with echo -e and od , but it did not work.

0xA7.0x9B.0x46.0x8D.0x1E.0x52.0xA7.0x9B.0x7B.0x31.0xD2 

i would expect a printable string but it is not. it should be a clear text password. But from what you said i guess is encrypted or hashed (binary version)

7 Answers 7

$ echo 54657374696e672031203220330 | xxd -r -p Testing 1 2 3$ 

-r tells it to convert hex to ascii as opposed to its normal mode of doing the opposite

-p tells it to use a plain format.

Watch out! echo adds a newline char to the end of everything it prints. Technically, this is more correct: printf «%s» «54657374696e672031203220330» | xxd -r -p since it doesn’t add the newline char.

Also, for anyone wondering, here’s how to go from the binary string (ASCII chars) to ASCII hex: printf «%s» «Testing 1 2 3» | xxd -p . Output: 54657374696e67203120322033 . If you do echo instead, you get the stray hex 0a at the end, which is the newling \n char. echo «Testing 1 2 3» | xxd -p results in 54657374696e672031203220330a .

This code will convert the text 0xA7.0x9B.0x46.0x8D.0x1E.0x52.0xA7.0x9B.0x7B.0x31.0xD2 into a stream of 11 bytes with equivalent values. These bytes will be written to standard out.

TESTDATA=$(echo '0xA7.0x9B.0x46.0x8D.0x1E.0x52.0xA7.0x9B.0x7B.0x31.0xD2' | tr '.' ' ') for c in $TESTDATA; do echo $c | xxd -r done 

As others have pointed out, this will not result in a printable ASCII string for the simple reason that the specified bytes are not ASCII. You need post more information about how you obtained this string for us to help you with that.

How it works: xxd -r translates hexadecimal data to binary (like a reverse hexdump). xxd requires that each line start off with the index number of the first character on the line (run hexdump on something and see how each line starts off with an index number). In our case we want that number to always be zero, since each execution only has one line. As luck would have it, our data already has zeros before every character as part of the 0x notation. The lower case x is ignored by xxd, so all we have to do is pipe each 0xhh character to xxd and let it do the work.

The tr translates periods to spaces so that for will split it up correctly.

Источник

How to Convert Hex to ASCII Characters in Linux

A numbering system is defined as hexadecimal if its representation is to base 16. The combined numerals and alphabetic characters that make up the hexadecimal system are:

0 1 2 3 4 5 6 7 8 9 A B C D E F.

A hexadecimal numbering system is ideal for large digital systems as it can hold/represent long binary values. This system is referred to as base-16 because a combined total of 16 (digital and alphabetic) symbols from 0 to F are used to represent it.

Читайте также:  Create zip archive on linux

In comparison to other numbering systems like decimal, hexadecimal provides a closer visual mapping making it easier to read ad interpret.

ASCII or American Standard Code for Information Interchange makes electronic communication possible through its character encoding standard. Therefore, this standard takes credit for text representation in devices like computers and telecommunication equipment.

For instance, to convert something like a user resume to ASCII format, that final document will be in plain text with no text formatting like underscores, bold, or tabs present. This raw format is easily processed by computers. It also makes it easy for the final document to be imported and/or used by other applications.

By the end of this article, we should be able to comfortably convert Hex to ASCII characters via Linux.

Problem Statement

Consider the following Hex characters:

54657374696e672031203220330

Theoretically, the equivalent translation of the above Hex characters to ASCII characters is:

We are going to look at various Linux-based approaches to arriving at the above-stated solution.

Method 1: Using echo and xxd Commands

We mainly use the xxd command to make a hexdump of sampled Hex characters or reverse the stated action. Firstly, an echo command will take the sample Hex characters as inputs before piping them to the xxd command for for conversion to ASCII characters.

$ echo 54657374696e672031203220330 | xxd -r -p && echo ''

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.

Convert Hex to ASCII in Linux

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.

$ cat sample_file.txt $ cat sample_file.txt | xxd -r -p && echo ''

Convert Hex to ASCII from File

Method 2: Using printf Command

The printf command formats and prints data input in respect to the specified data format. The command option \x takes Hex character inputs of 1 to 2 digits as demonstrated below:

$ printf '\x54\x65\x73\x74\x69\x6e\x67\x20\x31\x20\x32\x20\x33\x0' && echo '' Testing 1 2 3 

Method 3: Using sed Command

The sed command is used to filter and transform the Hex characters input by passing it via a regular expression for conversion to ASCII character before the final output is printed.

$ echo -n 54657374696e67203120322033 | sed 's/\([0-9A-F]\\)/\\\\\\x/gI' | xargs printf && echo '' Testing 1 2 3 

We can now comfortably convert simply complex Hex to ASCII characters from the Linux command-line environment.

Источник

Linux shell scripting: hex number to binary string

I am looking for some easy way in shell script for converting hex number into sequence of 0 and 1 characters. Example:

5 Answers 5

echo "ibase=16; obase=2; 5F" | bc 

This is not the same as the selected answer. This has 5F , whereas the selected answer has 5f . This answer works in bc 1.06.95, wheras the selected answer does not.

I used ‘bc’ command in Linux. (much more complex calculator than converting!)

Читайте также:  Debian linux intel atom

ibase parameter is the input base (hexa in this case), and obase the output base (binary).

Set obase before ibase . If ibase is defined first, bc will try to interpret obase as if it is written in ibase , with possibly erroneous results. See this question.

Using bc version 1.06.95 echo ‘ibase=16;obase=2;5f’ | bc produces error: (standard_in) 1: syntax error . However, echo ‘ibase=16;obase=2;5F’ | bc works and produces 1011111 .

$ printf '\x5F' | xxd -b | cut -d' ' -f2 01011111 
  • The i command will pop the top of the stack and use it for the input base.
  • Hex digits must be in upper case to avoid collisions with dc commands and are not limited to A-F if the input radix is larger than 16 .
  • The o command does the same for the output base.
  • The p command will print the top of the stack with a newline after it.

Thanks for response, it’s great to have so many ways of achieving this task 🙂 I’ve chosen the «bc» variant

Perl’s printf already knows binary:

$ perl -e 'printf "%08b\n", 0x5D' 01011101 
echo -n 5f5f5f5f5f | cryptocli dd -decoders hex -encoders binary_string 
0101111101011111010111110101111101011111 

NB: It’s not perfect and much work needs to be done but it is working.

Linked

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.7.14.43533

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Источник

Convert Hex to ASCII Characters in Linux Command Line

Here are various ways for converting Hex to ASCII characters in the Linux command line and bash scripts.

Got a bunch of hexadecimal characters and want to convert them to a readable decimal system (ASCII)?

There are multiple ways to convert hex to ASCII in Linux. You may also use these methods in your shell script if required.

Converting Hexadecimal to ASCII in Linux

Hexadecimal is a number system in which you use a combination of numbers (0-9) to represent values ranging from 0 to 9 and alphabets (A-F) to represent values from 10 to 15.

It may sound complicated but it is still better than binary numbering where everything is 1 and 0.

Hex was extensively used in microcontrollers a few decades ago. You could still see it in use, especially with the Hex color pallet which is used to represent various shades of colors.

On the other hand, ASCII stands for American Standard Code for Information Interchange and has a completely different use case than Hexadecimal. ASCII is a method that is used to convert normal language characters to binary so computers can understand instructions from our side.

Through this guide, I will include various ways by which you can easily convert hex to ASCII.

Convert hex to ascii in Linux

Let’s consider the following hex code (problem statement) which I want to convert:

4c696e757868616e64626f6f6b20313233

And if I succeed in converting the above hex code, the ASCII conversion should look like this:

Method 1: Using xxd Command

xxd is a command-line utility that can create a hex dump from the given text and vice versa.

Читайте также:  Linux распаковать все файлы

First I’m going to input hex string using echo command and will pair it with xxd using pipe.

echo 4c696e757868616e64626f6f6b20313233 | xxd -r -p

Here, command option -r is used to convert hex code to ASCII, and -p is used to print the plain text.

[email protected]itsfoss:~$ echo 4c696e757868616e64626f6f6b20313233 | xxd -r -p Linuxhandbook [email protected]:~$ 

As you can see above, the output gets mixed with the prompt in the terminal because the converted output doesn’t have the new line character. To make it more readable, you may append echo » to the above command.

echo 4c696e757868616e64626f6f6b20313233 | xxd -r -p && echo ''

Method 2: Using printf Command

Yes, you can convert hex to ASCII using the bash printf command. Here, I’m going to use the \x option which will take input of 1 or two digits as shown below:

printf '\x4c\x69\x6e\x75\x78\x68\x61\x6e\x64\x62\x6f\x6f\x6b\x20\x31\x32\x33'

There is no issue if you get a single digit at the end of the hex string and it will work fine as demonstrated.

Method 3: Using dc Command

You might be wondering how we can use deck calculator (dc) for converting hex to ASCII and I will show you exactly how.

echo "16i 4C696E757868616E64626F6F6B20313233 P" | dc

Here, 16i is used to indicate that you are dealing with hex radix, and P is used to print output from the dc command.

By using the above command, you will get a similar result as given below:

Method 4: Using Perl Command

So if your system has Perl installed, you can easily convert hex to ASCII using this method. Just interchange my hex code with yours in the given command and that’s it.

echo 4c696e757868616e64626f6f6b20313233 | perl -ne 's/([0-9a-f])/print chr hex $1/gie' && echo ''

And you’ll be met with the results as shown below:

Method 5: Using sed Command

This method is a bit similar to the above but here you are using sed to filter and convert hex to ASCII using normal expression.

echo -n 4c696e757868616e64626f6f6b20313233 | sed 's/\([0-9A-F]\\)/\\\\\\x\1/gI' | xargs printf && echo '' 

By using piping, we collected hash string through echo, converted it by pairing sed to given expression, and got output as given.

Method 6: Convert hex strings stored in text file

So if you have a hex string stored under a text file, you can use this method to convert the stored hex string to ASCII.

My hex string is stored in a hex.txt file so to convert that string, I’ll have to follow the given command:

cat hex.txt | xxd -r -p && echo ''

By far this is the most convenient way as you don’t have to enter hex strings again and again and the best part is you can store multiple hex strings and convert them using the same command.

This was my take on how you can convert hex string to ASCII using various methods. By far, the last one is my favorite as it is quite efficient compared to the others.

There are dedicated Hex editors in Linux and you may try them as well if you want.

Источник

Оцените статью
Adblock
detector