【问题标题】:How to iterate through all ASCII characters in Bash?如何遍历 Bash 中的所有 ASCII 字符?
【发布时间】:2012-10-19 03:22:33
【问题描述】:

我知道如何遍历字母:

for c in {a..z}; do ...; done

但我不知道如何遍历所有 ASCII 字符。有人知道怎么做吗?

【问题讨论】:

    标签: linux bash ascii


    【解决方案1】:

    您可以做的是从 0 到 127 进行迭代,然后将十进制值转换为其 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-31 通常不被认为是“可打印的”,除了空白字符(回车、换行以及水平和垂直制表符)。
    【解决方案2】:

    仅使用echos 八进制转义序列的可能性:

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

    【讨论】:

      【解决方案3】:

      这是我从 sampson-chen 和 mata 的答案中提取一些片段的单行代码:

      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); }'
      

      【讨论】:

        【解决方案4】:

        以下是使用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
        

        【讨论】:

          【解决方案5】:

          嗯...如果你真的想要它们,并且你希望它像脚本一样,你可以这样做,我猜:

          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 的二进制整数的最优雅的方法 - 如果你发现它,可以替换一些更优雅的方法。

          编辑:哦,我知道你只想要 ascii。好吧,我会让这个答案留在这里,以防其他人真的想要所有 UTF 可打印字符。

          【讨论】:

            【解决方案6】:

            这取决于您所说的迭代。请注意,NUL 不能分配或传递给命令。

            这会生成所有 ascii 字符

            seq 0 127 |\
             xargs printf '\\x%x ' |\
             xargs printf '%b '
            
            • seq 0 127 生成 0 到 127 之间的所有整数
            • xargs printf '\\x%x ' 转成十六进制,用空格隔开
            • xargs printf '%b ' 将十六进制转换为字节,以空格分隔

            【讨论】:

              猜你喜欢
              • 2015-06-18
              • 1970-01-01
              • 2016-02-20
              • 1970-01-01
              • 2018-06-24
              • 1970-01-01
              • 2016-01-04
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多