linux – 如何遍历Bash中的所有ASCII字符?

linux – 如何遍历Bash中的所有ASCII字符?,第1张

概述我知道如何迭代字母表: for c in {a..z}; do ...; done 但我无法弄清楚如何遍历所有ASCII字符.有谁知道怎么样? 你可以做的是从0迭代到127然后将十进制值转换为它的ASCII值(或返回). 您可以使用these函数执行此 *** 作: # POSIX# chr() - converts decimal value to its ASCII character repres 我知道如何迭代字母表:
for c in {a..z}; do ...; done

但我无法弄清楚如何遍历所有ASCII字符.有谁知道怎么样?

解决方法 你可以做的是从0迭代到127然后将十进制值转换为它的ASCII值(或返回).

您可以使用these函数执行此 *** 作:

# POSIX# chr() - converts decimal value to its ASCII character representation# ord() - converts ASCII character to its decimal valuechr() {  [  -lt 256 ] || return 1  printf \$(printf '%03o' )}# Another version doing the octal conversion with arithmetic# faster as it avoIDs a subshellchr () {  [  -lt 256 ] || return 1  printf \$((/64*100+%64/8*10+%8))}# Another version using a temporary variable to avoID subshell.# This one requires bash 3.1.chr() {  local tmp  [  -lt 256 ] || return 1  printf -v tmp '%03o' ""  printf \"$tmp"}ord() {  LC_CTYPE=C printf '%d' "'"}# hex() - converts ASCII character to a hexadecimal value# unhex() - converts a hexadecimal value to an ASCII characterhex() {   LC_CTYPE=C printf '%x' "'"}unhex() {   printf \x""}# examples:chr $(ord A)    # -> Aord $(chr 65)   # -> 65
总结

以上是内存溢出为你收集整理的linux – 如何遍历Bash中的所有ASCII字符?全部内容,希望文章能够帮你解决linux – 如何遍历Bash中的所有ASCII字符?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/1047968.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存