使用 printf 将整数 ASCII 值转换为 BASH 中的字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/890262/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Integer ASCII value to character in BASH using printf
提问by
Character to value works:
价值作品的性格:
$ printf "%d\n" \'A
65
$
I have two questions, the first one is most important:
我有两个问题,第一个是最重要的:
- How do I take 65 and turn it into A?
- \'A converts an ASCII character to its value using printf. Is the syntax specificto printfor is it used anywhere else in BASH? (Such small strings are hard to Google for.)
- 我如何取 65 并将其变成 A?
- \'A 使用 printf 将 ASCII 字符转换为其值。特定于printf的语法还是在 BASH 中的其他任何地方使用?(这么小的字符串很难用谷歌搜索。)
回答by broaden
One line
一条线
printf "\x$(printf %x 65)"
Two lines
两条线
set $(printf %x 65)
printf "\x"
Here is one if you do not mind using awk
如果您不介意使用,这里是一个 awk
awk 'BEGIN{printf "%c", 65}'
回答by broaden
This works (with the value in octal):
这有效(使用八进制值):
$ printf '%b' '1'
A
even for (some: don't go over 7) sequences:
即使对于(某些:不要超过 7 个)序列:
$ printf '%b' '\'{101..107}
ABCDEFG
A general construct that allows (decimal) values in any range is:
允许任何范围内的(十进制)值的一般构造是:
$ printf '%b' $(printf '\%03o' {65..122})
ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz
Or you could use the hex values of the characters:
或者您可以使用字符的十六进制值:
$ printf '%b' $(printf '\x%x' {65..122})
ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz
You also could get the character back with xxd (use hexadecimal values):
您还可以使用 xxd 取回字符(使用十六进制值):
$ echo "41" | xxd -p -r
A
That is, one action is the reverse of the other:
也就是说,一个动作与另一个动作相反:
$ printf "%x" "'A" | xxd -p -r
A
And also works with several hex values at once:
并且还可以同时处理多个十六进制值:
$ echo "41 42 43 44 45 46 47 48 49 4a" | xxd -p -r
ABCDEFGHIJ
or sequences (printf is used here to get hex values):
或序列(此处使用 printf 来获取十六进制值):
$ printf '%x' {65..90} | xxd -r -p
ABCDEFGHIJKLMNOPQRSTUVWXYZ
Or even use awk:
或者甚至使用 awk:
$ echo 65 | awk '{printf("%c",)}'
A
even for sequences:
即使对于序列:
$ seq 65 90 | awk '{printf("%c",)}'
ABCDEFGHIJKLMNOPQRSTUVWXYZ
回答by David Hu
For your second question, it seems the leading-quote syntax (\'A
) is specific to printf
:
对于您的第二个问题,前导引号语法 ( \'A
)似乎特定于printf
:
If the leading character is a single-quote or double-quote, the value shall be the numeric value in the underlying codeset of the character following the single-quote or double-quote.
如果前导字符是单引号或双引号,则该值应是单引号或双引号之后字符的底层代码集中的数值。
From http://pubs.opengroup.org/onlinepubs/009695399/utilities/printf.html
来自http://pubs.opengroup.org/onlinepubs/009695399/utilities/printf.html
回答by mouviciel
For this kind of conversion, I use perl:
对于这种转换,我使用 perl:
perl -e 'printf "%c\n", 65;'
回答by Naaff
One option is to directly input the character you're interested in using hex or octal notation:
一种选择是使用十六进制或八进制表示法直接输入您感兴趣的字符:
printf "\x41\n"
printf "1\n"
回答by MagicMercury86
If you want to save the ASCII value of the character: (I did this in BASH and it worked)
如果你想保存字符的 ASCII 值:(我在 BASH 中做了这个并且它有效)
{
char="A"
testing=$( printf "%d" "'${char}" )
echo $testing}
output: 65
输出:65
回答by Cyber Oliveira
If you convert 65
to hexadecimal it's 0x41
:
如果转换65
为十六进制,则为0x41
:
$ echo -e "\x41"
A
$ echo -e "\x41"
A
回答by chand
Here's yet another way to convert 65 into A (via octal):
这是将 65 转换为 A 的另一种方法(通过八进制):
help printf # in Bash
man bash | less -Ip '^[[:blank:]]*printf'
printf "%d\n" '"A'
printf "%d\n" "'A"
printf '%b\n' "$(printf '\%03o' 65)"
To search in man bash
for \'
use (though futile in this case):
以搜索man bash
为\'
使用(虽然徒劳在这种情况下):
man bash | less -Ip "\\'" # press <n> to go through the matches
回答by Cyrus
For capital letters:
对于大写字母:
i=67
letters=({A..Z})
echo "${letters[$i-65]}"
Output:
输出:
C