33

was wondering if anyone could help me with converting ascii to hex in bash. Example code:

#!/bin/bash 
STR = "hello"
#Convert to hex
HEXVAL = $STR #(in hex here?)

I want hexval to have the value: 68656C6C6F (hello in hex)

user1739261
  • 339
  • 1
  • 3
  • 5
  • 1
    Welcome to stackoverflow. [Have you already tried anything?](http://whathaveyoutried.com) People are more eager to help if you show some research effort before asking. Otherwise, you will appear as a [help vampire](http://slash7.com/2006/12/22/vampires/) – JMax Oct 11 '12 at 19:52
  • possible dupe of http://stackoverflow.com/questions/5724761/ascii-hex-convert-in-bash – mrchampe Oct 11 '12 at 19:57

7 Answers7

50
$ str="hello"
$ hex="$(printf '%s' "$str" | xxd -p -u)"
$ echo "$hex"
68656C6C6F

Or:

$ hex="$(printf '%s' "$str" | hexdump -ve '/1 "%02X"')"
$ echo "$hex"
68656C6C6F

Careful with the '"%X"'; it has both single quotes and double quotes.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
11

You have several options

$ printf hello | xxd
0000000: 6865 6c6c 6f                             hello

See also: Ascii/Hex convert in bash

Community
  • 1
  • 1
mrchampe
  • 452
  • 3
  • 12
4

Pure BASH convertor of string to printable hexadecimal sequence and back

str2hex_echo() {
    # USAGE: hex_repr=$(str2hex_echo "ABC")
    #        returns "0x410x420x43"
    local str=${1:-""}
    local fmt="0x%x"
    local chr
    local -i i
    for i in `seq 0 $((${#str}-1))`; do
        chr=${str:i:1}
        printf  "${fmt}" "'${chr}"
    done
}

hex2str_echo() {
    # USAGE: ASCII_repr=$(hex2str_echo "0x410x420x43")
    #        returns "ABC"
    echo -en "'${1:-""//0x/\\x}'"
}

EXPLANATION

ASCII->hex: The secret sauce of efficient conversion from character to its underlying ASCII code is feature in printf that, with non-string format specifiers, takes leading character being a single or double quotation mark as an order to produce the underlying ASCII code of the next symbol. This behavior is documented in GNU BASH reference, but is also exposed in details together with many other other wonderful utilities in Greg's (also known as GreyCat's) wiki page BashFAQ/071 dedicated to char-ASCII conversions.

makarevs
  • 41
  • 2
1

here's a one liner (a little complex but works fine):

#!/bin/bash

echo '0x'"`echo $1 | hexdump -vC |  awk 'BEGIN {IFS="\t"} {$1=""; print }' | awk '{sub(/\|.*/,"")}1'  | tr -d '\n' | tr -d ' '`" | rev | cut -c 3- | rev
1
xxd -p -u <<< "$STR" | sed 's/\(..\)/0x&, /g; s/, $//;'

0x68, 0x65, 0x6C, 0x6C, 0x6F, 0x0A

anthony
  • 7,696
  • 1
  • 17
  • 11
0

Only hex line output. Tested, both systems verified.

# Linux
$ echo -n 'hello' | hexdump -ve '/1 "%02x"'
68656c6c6f
# Mac
$ echo -n 'hello' | xxd -p -c 256
68656c6c6f
storenth
  • 967
  • 11
  • 18
0

POSIX option

I think this one is POSIX:

od-raw() {
  od -A n -t x1 -v | tr -d ' \n' 
}

Usage:

printf hello | od-raw

Output:

68656c6c6f

Or if you want separating spaces:

os-raw-spaces() {
  od -A n -t x1 -v "$@" | tr -d '\n' | cut -c 2-
}

Output:

68 65 6c 6c 6f

Tested on Ubuntu 22.04.

Related:

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985