击.十六进制到ascii.没有xxd或perl可能吗?

Osc*_*vis 1 bash perl awk hex ascii

我正在开发一个脚本,我有一个十六进制字符串31323334353637383930313233,我想将其转换为ASCII.期望的输出是1234567890123.

我已经使用它了:

echo "31323334353637383930313233" | xxd -r -p
Run Code Online (Sandbox Code Playgroud)

要么

echo "31323334353637383930313233" | perl -pe 's/(..)/chr(hex($1))/ge'
Run Code Online (Sandbox Code Playgroud)

但重点是尝试使用脚本的最低可能要求.我希望它在suse,fedora,debian,ubuntu,arch等工作......似乎xxd命令包含在vim包中.我想知道是否有办法使用awk或任何内部Linux工具来实现这一点,默认情况下将在所有Linux系统中使用.

谢谢.

Zum*_*rio 5

在这里找到这个脚本:

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

您可以更改该行hex2string "31323334353637383930313233",使其从参数中获取十六进制值,即:

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

所以当执行时:

./hexstring.sh 31323334353637383930313233
Run Code Online (Sandbox Code Playgroud)

它将提供所需的ascii输出.

注意:无法测试它是否适用于所有Linux系统.


Aks*_*gde 5

使用,从HEXASCII

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

使用任何

$ 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 ""
}
Run Code Online (Sandbox Code Playgroud)

执行

$ awk -f hex2asc_anyawk.awk <<<"31323334353637383930313233"
1234567890123
Run Code Online (Sandbox Code Playgroud)

说明

脚步 :

  1. 从表中获取十六进制的十进制等效值。

  2. 将每个数字乘以16的数字位置幂。

  3. 对所有乘数求和。

在此处输入图片说明

范例:

在此处输入图片说明

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 ""
}
Run Code Online (Sandbox Code Playgroud)

的意思 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 
Run Code Online (Sandbox Code Playgroud)