Sounds to me like you don't really need all the characters...

$ awk 'BEGIN { chartable="abcdefghij" ; for (i=index(chartable, "a"); i<index(chartable, "g"); i++) { print substr(chartable, i, 1) } }'
a
b
c
d
e
f
Answer from Ignacio Vazquez-Abrams on Stack Exchange
🌐
Tek-Tips
tek-tips.com › home › forums › software › programmers › languages › awk
convert hex to ascii - AWK
December 20, 2007 - It is an awk function. Include it at the beginning of your awk script, and call it using hex2dec(value), e.g.
Discussions

xxd - awk command to map ASCII char to hex code not working - Unix & Linux Stack Exchange
I'm experimenting with awk and I thought it would be a cool idea to write a small program that maps every ASCII char with its hex code. this is what ive done so far: The string is: More on unix.stackexchange.com
🌐 unix.stackexchange.com
Convert column from hex to ascii
I can modify the column using awk but I can't pass that value to another command to do the convertion. Either you can do whatever you want, or this is homework. If so, please say so. Next, is this a Bash-only assignment, or can you use other languages? This is easy to do, but it would be much easier if you would post the assignment's hexadecimal source so I could show the conversion of your example text. Meanwhile, here's a sample: #!/usr/bin/env bash ascii="" for c in 54 68 69 73 20 69 73 20 61 20 74 65 73 74 2e; do ascii+="\x$c" done echo -e $ascii Output: This is a test. More on reddit.com
🌐 r/bash
21
5
July 25, 2019
convert hex to ascii
Greetings, I am looking for direction. I have some xml files that contain a couple fields data that are in HEX format. I was hoping that AWK would allow me to convert the HEX fields to ascii. The end product would hopefully be the a new xml file with only the hex having been converted to... More on tek-tips.com
🌐 tek-tips.com
5
0
December 20, 2007
Bash. Hex to ascii. Is possible without xxd or perl? - Stack Overflow
I'm developing a script on which I have a hex string 31323334353637383930313233 and I want to transform it into ASCII. Desired output is 1234567890123. ... But the point is try to use the minimum possible requirements for the script. I want it working in suse, fedora, debian, ubuntu, arch, etc... It seems the xxd command is included in vim package. I'm wondering if there is a way to achieve this using only awk ... More on stackoverflow.com
🌐 stackoverflow.com
Top answer
1 of 5
3

No, \nnn is octal and \xnn is hex - that's all there is for including characters you cannot include as-is in strings and you should always use the octal, not the hex, representation for robustness (see, for example, http://awk.freeshell.org/PrintASingleQuote).

I don't understand the last part of your question where you state what you're trying to do with this - provide concise, testable sample input and expected output and I'm sure someone can help you do it the right way, whatever it is.

Is this what you're trying to do?

$ awk 'BEGIN{for (i=0141; i<0143; i++) print i}'
97
98
2 of 5
1

A lookup table is the only way to address this (directly convert CHAR to ASCII DECIMAL) within "AWK only".

You can simply use sprintf() to convert ASCII DECIMAL to CHAR.

  • You can create a lookup table by iterating through each of the known ascii chars and storing them in an array where the key is the character and the value is the ascii value of that char.

  • You can use sprintf() within AWK to get the char for each decimal.

  • Then you can pass the char to the array to get the corresponding decimal again.

In this example, using awk.

  • We cycle through all 256 characters, printing out each one.
  • We split the resulting string into a series of lines where each line has a single character.
  • We build a table in awk of the 256 characters (in BEGIN), and then feed each of the input characters in to lookup each one.
  • Finally we then print out the code for each character on the input.
awk 'BEGIN{
    for(n=0;n<256;n++)
        print sprintf("%c",n)
}' | awk '{
for (i=0; ++i <= length($0);)
    printf "%s\n", substr($0, i, 1)
}' | awk 'BEGIN{
    for(n=0;n<256;n++)
        ord[sprintf("%c",n)]=n
}{
    print ord[$1]
}'

The reverse can also be done, where we lookup a list of character codes.

awk 'BEGIN{
    for(n=0;n<256;n++)
        print sprintf("%s",n)
}' | awk 'BEGIN{
    for(n=0;n<256;n++)
        char[n]=sprintf("%c",n)
}{
    print char[$1]
}'

Note: The second example may print out a lot of garbage in the high ascii range (> 128) depending on the character set you are using.

🌐
Malwaremusings
malwaremusings.com › scripts › unhex-awk
unhex.awk: Convert hex encoded strings to ASCII characters | Malware Musings
# # Non-printable characters are converted to a ‘?’. # # # # unhex.awk v2013.04.01 # # http://malwaremusings.com/scripts/unhex-awk # # # # usage: awk -f unhex.awk <inputfilename> # # where inputfilename is a text file containing hex # # encoded (0x…) strings. # ######################################################## /0x/ { while (s = match($0,"0[Xx][0-9A-Fa-f]+",matcharray) > 0) { chrstr = ""; hexdigits = "0123456789abcdef"; word = tolower(matcharray[0]); value = 0; for (nibble = 3;nibble <= length(word);nibble++) { char = substr(word,nibble,1); idx = index(hexdigits,char) – 1; value
Top answer
1 of 4
2

It sounds like your goal is to learn (about) awk rather than to simply get the ASCII table, which per other answer is easily available.

You don't need any input file(s) for this; you can directly display all the printable characters with

awk 'BEGIN{ for(i=32;i<127;i++) printf("%02x %c\n",i,i) }'

(This generates one long column, as your attempt did; if you want multicolumns that's left as an exercise.)

OTOH if you want to combine pairs of fields from two lines -- which can be any values not just a char and its hex:

awk [-Fasneeded] 'NR==1 { for(i=1;i<=NF;i++) save[i]=$i; next }
  { for(i=1;i<=NF;i++) print save[i],$i }' file ...
# linebreak for ease of reading, may be omitted in use

This works whether the two lines come from two files, as in your case, or one file. If you have more than two lines, this pairs fields from line 1 to each subsequent line, a pattern that often makes sense for CSV-type files which have a header on line 1 with names for each column, followed by a variable number of lines with data in each column.

This also preserves the order including any dupes. To eliminate dupes but otherwise keep order:

awk [-Fasneeded] 'NR==1 { for(i=1;i<=NF;i++) save[i]=$i; next } 
  { for(i=1;i<=NF;i++) if(!dupe[$i]++) print save[i],$i ;delete dupe }' file ...
# the delete dupe can be omitted in the two-line case

If you actually want the random order created by for in that's also possible, but it makes no sense to me. Some useful order such as numerical order or alphabetical order (with dupes eliminated) might make sense.

2 of 4
2

If you use the GNU awk, then you can include the extended library ordchr:

echo abshdfitiggwigiwjirjgiejrigjr |
    awk -lordchr -F '' '{for(i=1;i<=NF;i++)printf "%c %x\n", i)}'
a 61
b 62
s 73
h 68
...

ord() - function, convert char to decimal
printf "%x", ord($i) - convert decimal to hex
Additionally: chr() - convert decimal to char

🌐
Tek-Tips
tek-tips.com › home › forums › software › programmers › languages › awk
convert hex to ascii | Tek-Tips
December 20, 2007 - It is an awk function. Include it at the beginning of your awk script, and call it using hex2dec(value), e.g.
Top answer
1 of 5
4

Found this script here:

#!/bin/bash
function hex2string () {
  I=0
  while [ $I -lt ${#1} ];
  do
    echo -en "\x"${1:$I:2}
    let "I += 2"
  done
}
hex2string "31323334353637383930313233"
echo

You may change the line hex2string "31323334353637383930313233" so that it takes the hex value from parameters, that is:

#!/bin/bash
function hex2string () {
  I=0
  while [ $I -lt ${#1} ];
  do
    echo -en "\x"${1:$I:2}
    let "I += 2"
  done
}
hex2string "$1"
echo

So when executed as:

./hexstring.sh 31323334353637383930313233

It will provide the desired ascii output.

NOTE: Can't test if it works in all Linux systems.

2 of 5
4

Using gawk, from HEX to ASCII

$ gawk  '{ 
           gsub(/../,"0x& "); 
           for(i=1;i<=NF;i++)
              printf("%c", strtonum($i)); 
           print "" 
}' <<<"31323334353637383930313233"
1234567890123

Using any awk

$ cat hex2asc_anyawk.awk 
BEGIN{
    split("0 1 2 3 4 5 6 7 8 9 A B C D E F", d, / /)
    for(i in d)Decimal[d[i]]=i-1
}

function hex2dec(hex,  h,i,j,dec)
{ 
    hex = toupper(hex);                
    i   = length(hex);                 
    while(i)
    {  
          dec += Decimal[substr(hex,i,1)] * 16 ^ j++
          i--                          
    }
    return dec;
}
{

    gsub(/../,"& "); 
    for(i=1;i<=NF;i++)
        printf("%d",hex2dec($i));
    print ""
}

Execution

$ awk -f hex2asc_anyawk.awk <<<"31323334353637383930313233"
1234567890123

Explanation

Steps :

  1. Get the decimal equivalent of hex from table.

  2. Multiply every digit with 16 power of digit location.

  3. Sum all the multipliers.

Example :

BEGIN{
    # Here we created decimal conversion array, like above table
    split("0 1 2 3 4 5 6 7 8 9 A B C D E F", d, / /)
    for(i in d)Decimal[d[i]]=i-1
}

function hex2dec(hex,  h,i,j,dec)
{ 
    hex = toupper(hex);                 # uppercase conversion if any A,B,C,D,E,F
    i   = length(hex);                  # length of hex string
    while(i)
    {  
          # dec var where sum is stored
          # substr(hex,i,1) gives 1 char from RHS
          # multiply by 16 power of digit location 
          dec += Decimal[substr(hex,i,1)] * 16 ^ j++
          i--                           # decrement by 1
    }
    return dec;
}
{
    # it modifies record
    # suppose if given string is 31323334353637383930313233
    # after gsub it becomes 31 32 33 34 35 36 37 38 39 30 31 32 33
    # thus re-evaluate the fields

    gsub(/../,"& "); 

    # loop through fields , NF gives no of fields
    for(i=1;i<=NF;i++)

        # convert from hex to decimal
        # and print equivalent ASCII value 
        printf("%c",hex2dec($i));

    # print newline char
    print ""
}

Meaning of dec += Decimal[substr(hex,i,1)] * 16 ^ j++

dec += Decimal[substr(hex,i,1)] * 16 ^ j++
 ^           ^                    ^
 |           |                    | 
 |           |                    2.Multiply every digit with 16 power of digit location.
 |           |    
 |           1.Gives decimal equivalent of hex
 | 
 | 
 3. Sum all the multipliers 
Find elsewhere
🌐
Unix Community
community.unix.com › shell programming and scripting
Converting hex to ascii/decimal - Shell Programming and Scripting - Unix Linux Community
March 31, 2011 - I am writing a bash script to do some parsing on a log and I am running into a problem when it comes to converting only certain sections of the file from hex to ascii or hex to decimal. Data Example: The hex values af…
🌐
UbuntuMint
ubuntumint.com › home › linux commandline tips › how to convert hex to ascii characters in linux
How to Convert Hex to ASCII Characters in Linux
July 24, 2023 - The command option -r converts the Hex characters to ASCII and command option -p prints the outcome in plain text.
🌐
Raspberry Pi Forums
forums.raspberrypi.com › board index › using the raspberry pi › beginners
Need help converting hex to ascii - Raspberry Pi Forums
... Assuming you have a file called hex.txt and the contents looks something like below with no spaces before or after each hex code: 0x43 0x39 0x39 0x30 0x31 0x30 then the following should work: echo -e `cat hex.txt | awk '{ printf "\x" substr($0,3) }'` or: echo -e `cat hex.txt | awk '{ printf ...
🌐
Google Groups
groups.google.com › g › comp.lang.awk › c › Qmi2sXJmqAM
hex string to decimal number
In article <slrn5ea4l...@kedemel.demon.co.uk>, Duncan Square wrote: > >Im trying to convert a hexadecimal string into a decimal number using awk, >and not succeeding. > The printf command in awk can convert a decimal number to hex, octal, or ascii, but it can't convert to decimal.
🌐
LinuxQuestions.org
linuxquestions.org › questions › programming-9 › [bash]-ascii-to-hex-and-hex-to-ascii-488357
[bash] ASCII to HEX and hex to ascii
What kind of command should I use to translate ascii to hex in a bash script ? (and hex back to ascii) TIA /////
🌐
Hexcalculator
hexcalculator.org › hex-to-decimal-in-awk
Hex to Decimal in AWK
May 29, 2026 - If you’re working with hex in AWK, these related tools can help: Hex to ASCII Converter – decode text stored in hex