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

时间:2017-03-16 07:57:35

标签: bash perl awk hex ascii

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

我已经使用它了:

echo "31323334353637383930313233" | xxd -r -p

echo "31323334353637383930313233" | perl -pe 's/(..)/chr(hex($1))/ge'

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

谢谢。

3 个答案:

答案 0 :(得分:5)

找到此脚本.filter

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 ,使其从参数中获取十六进制值,即:

./hexstring.sh 31323334353637383930313233

所以当执行时:

from Tkinter import *

def printSomething():
    # if you want the button to disappear:
    # button.destroy() or button.pack_forget()
    label = Label(root, text= "Hey whatsup bro, i am doing something very interresting.")
    #this creates a new label to the GUI
    label.pack() 

root = Tk()

button = Button(root, text="Print Me", command=printSomething) 
button.pack()

root.mainloop()

它将提供所需的ascii输出。

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

答案 1 :(得分:4)

使用,从HEXASCII

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

使用任何

$ 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 ""
}

<强>执行

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

<强>解释

步骤:

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

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

  3. 求和所有乘数。

  4. enter image description here

    示例:

    enter image description here

    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 ""
    }
    
      

    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 
    

答案 2 :(得分:1)

替代(g)awk解决方案:

echo "31323334353637383930313233" | awk 'RT{printf "%c", strtonum("0x"RT)}' RS='[0-9]{2}'