2012-10-29 8 views

答えて

7

あなたができることは、iterate frです。 om 0〜127を入力し、10進値をそのASCII値に変換します。

あなたはそれを行うにはthese機能を使用することができます。

まあ
# POSIX 
# chr() - converts decimal value to its ASCII character representation 
# ord() - converts ASCII character to its decimal value 

chr() { 
    [ ${1} -lt 256 ] || return 1 
    printf \\$(printf '%03o' $1) 
} 

# Another version doing the octal conversion with arithmetic 
# faster as it avoids a subshell 
chr() { 
    [ ${1} -lt 256 ] || return 1 
    printf \\$(($1/64*100+$1%64/8*10+$1%8)) 
} 

# Another version using a temporary variable to avoid subshell. 
# This one requires bash 3.1. 
chr() { 
    local tmp 
    [ ${1} -lt 256 ] || return 1 
    printf -v tmp '%03o' "$1" 
    printf \\"$tmp" 
} 

ord() { 
    LC_CTYPE=C printf '%d' "'$1" 
} 

# hex() - converts ASCII character to a hexadecimal value 
# unhex() - converts a hexadecimal value to an ASCII character 

hex() { 
    LC_CTYPE=C printf '%x' "'$1" 
} 

unhex() { 
    printf \\x"$1" 
} 

# examples: 

chr $(ord A) # -> A 
ord $(chr 65) # -> 65 
+0

0-31は通常「印刷可能」とはみなされません。ただし、th e空白文字(改行、改行、水平および垂直タブ)。 – twalberg

3

ここでは、awkとそれに対応するASCII文字として整数を印刷する方法は次のとおりです。

印刷します
echo "65" | awk '{ printf("%c", $0); }' 

A 

そして、ここでは、あなたは大文字のアルファベットを通じて、この方法を繰り返す可能性がある方法は次のとおりです。

# ascii for A starts at 65: 
ascii=65 
index=1 
total=26 
while [[ $total -ge $index ]] 
do 
    letter=$(echo "$ascii" | awk '{ printf("%c", $0); }') 
    echo "The $index'th letter is $letter" 

    # Increment the index counter as well as the ascii counter 
    index=$((index+1)) 
    ascii=$((ascii+1)) 
done 
2

...あなたが実際にそれらのすべてをしたい、とあなたはそれがスクリプトのようなものになりたい場合は、この操作を行うことができ、私は推測します:

awk 'function utf32(i) {printf("%c%c%c%c",i%0x100,i/0x100%0x100,i/0x10000%0x100,i/0x1000000) } BEGIN{for(i=0;i<0x110000;i++){utf32(i);utf32(0xa)}}' | iconv --from-code=utf32 --to-code=utf8 | grep -a '[[:print:]]' 

しかし、リストは非常に巨大であり、それほど有用ではありません。 Awkは、0から0x110000までの2進整数を生成するもっともエレガントな方法ではないかもしれません。

編集:ああ、私はあなたがasciiだけを望んでいるのを見ます。さて、誰かが実際にすべてのUTFの印刷可能な文字を望む場合に備えて、私はこの答えをここにとどめておきます。

5

のみechoの8進数のエスケープシーケンスを使用した可能性が:ここ

for n in {0..7}{0..7}{0..7}; do echo -ne "\\0$n"; done 
2

私はワンライナーは、サンプソン・チェンさんとマタの答えからのいくつかの作品を取るために思い付いたものです:

for n in {0..127}; do awk '{ printf("%c", $0); }' <<< $n; done 

または

for n in {0..127}; do echo $n; done | awk '{ printf("%c", $0); }' 
関連する問題