如何在 BASH / Shell 中将字符相互转换为十进制、二进制、八进制或十六进制表示形式?
printf
和 od
od
手册页:
printf
手册页:
single_ascii_char="A"
echo -n $single_ascii_char | od -A n -t d1
65
printf %d "'$single_ascii_char"
65
code=65
printf "\u$(printf %04x $code)\n" # use \u for up to 4 hexadecimal digits
A
printf "\U$(printf %08x $code)\n" # use \U for up to 8 hexadecimal digits
A
single_unicode_char="😈"
printf %d "'$single_unicode_char"
128520
echo -n $single_unicode_char | iconv -t UTF-32LE | od -A n -t d # d or u, d4, u4, dI, dL
128520 # or UTF-32BE, depending on system's endianness
code=128520
printf "\u$(printf %04x $code)\n" # use \u for up to 4 hexadecimal digits
ὠ8
printf "\U$(printf %08x $code)\n" # use \U for up to 8 hexadecimal digits
😈
single_ascii_char="A"
echo "obase=2; $(printf %d "'$single_ascii_char")" | bc
1000001
code="1000001"
printf "\u$(printf %04x $((2#$code)) )\n" # use \u for up to 4 hexadecimal digits
A
printf "\U$(printf %08x $((2#$code)) )\n" # use \U for up to 8 hexadecimal digits
A
single_unicode_char="😈"
echo "obase=2; $(printf %d "'$single_unicode_char")" | bc
11111011000001000
code="11111011000001000" # with or without leading 0s
printf "\u$(printf %04x $((2#$code)) )\n" # use \u for up to 4 hexadecimal digits
ὠ8
printf "\U$(printf %08x $((2#$code)) )\n" # use \U for up to 8 hexadecimal digits
😈
single_ascii_char="A"
printf %o "'$single_ascii_char"
101
echo -n $single_ascii_char | od -A n -t o1
101
code="\101"
printf %b "$code\n"
A
printf "$code\n"
A
single_unicode_char="😈"
printf %o "'$single_unicode_char"
373010
echo -n $single_unicode_char | iconv -t UTF-32LE | od -A n -t o # or o4
00000373010 # or UTF-32BE, depending on system's endianness
code="00000373010" # insert at least one leading 0 for printf to understand it's an octal
printf "\U$(printf %08x "$code")\n"
😈
echo -n "$single_unicode_char" | od -A n -t c # c or o1
360 237 230 210
code="\360\237\230\210"
printf %b "$code\n"
😈
printf "$code\n"
😈
single_ascii_char="A"
printf %x "'$single_ascii_char"
41
echo -n "$single_ascii_char" | od -A n -t x1
41
code="41"
printf "\u$code\n" # use \u for up to 4 hexadecimal digits
A
printf "\U$code\n" # use \U for up to 8 hexadecimal digits
A
single_unicode_char="😈"
printf %x "'$single_unicode_char"
1f608
printf %X "'$single_unicode_char"
1F608
echo -n $single_unicode_char | iconv -t UTF-32LE | od -A n -t x
0001f608 # or UTF-32BE, depending on system's endianness
code="1f608"
printf "\u$code\n" # use \u for up to 4 hexadecimal digits
ὠ8
printf "\U$code\n" # use \U for up to 8 hexadecimal digits
😈
printf %#x "'$single_unicode_char"
0x1f608
printf %#X "'$single_unicode_char"
0X1F608
code="0x1f608"
printf "\u$(printf %04x $code)\n" # use \u for up to 4 hexadecimal digits
ὠ8
printf "\U$(printf %08x $code)\n" # use \U for up to 8 hexadecimal digits
😈
echo -n "$single_unicode_char" | od -A n -t x1
f0 9f 98 88
code="\xf0\x9f\x98\x88"
printf %b "$code\n"
😈
printf "$code\n"
😈
bc
和 printf
ibase
和 obase
参数顺序很重要,但并非总是如此。十六进制值必须大写。echo "obase=16; 255" | bc
FF
echo "ibase=10; obase=16; 255" | bc
FF
echo "obase=16; ibase=10; 255" | bc
FF
printf "%x %08x %#x %#08x %X %08X %#X %#08X" {255,255,255,255,255,255,255,255}
ff 000000ff 0xff 0x0000ff FF 000000FF 0XFF 0X0000FF
echo "ibase=16; FF" | bc
255
echo "ibase=16; obase=10; FF" | bc # wrong
FF
echo "obase=10; ibase=16; FF" | bc
255
printf "%d " {0XFF,0X000000FF,0xff,0x000000ff}
255 255 255 255
echo $((16#FF))
255
echo "ibase=16; obase=2; FF" | bc
11111111
echo "obase=2; ibase=16; FF" | bc
11111111
echo "obase=16; ibase=2; 11111111" | bc
FF
echo "ibase=2; obase=16; 11111111" | bc # wrong
100110
echo "ibase=16; obase=8; FF" | bc
377
echo "obase=8; ibase=16; FF" | bc
377
printf "%o " {0XFF,0X000000FF,0xff,0x000000ff}
377 377 377 377
printf "%#o " {0XFF,0X000000FF,0xff,0x000000ff}
0377 0377 0377 0377
printf "%08o " {0XFF,0X000000FF,0xff,0x000000ff}
00000377 00000377 00000377 00000377
echo "obase=16; ibase=8; 377" | bc
FF
echo "ibase=8; obase=16; 377" | bc # wrong
143
printf "%x %08x %#x %#08x %X %08X %#X %#08X" {0377,0377,0377,0377,0377,0377,0377,0377}
ff 000000ff 0xff 0x0000ff FF 000000FF 0XFF 0X0000FF