在 BASH / Shell 中将字符与十进制、二进制、八进制或十六进制表示形式相互转换

问题描述 投票:0回答:1

如何在 BASH / Shell 中将字符相互转换为十进制、二进制、八进制或十六进制表示形式?

unicode binary hex endianness octal
1个回答
4
投票

使用
printf
od

在 BASH 中将字符从十进制、二进制、八进制或十六进制表示形式转换为字符
  1. 一些相关文档和Q&A:

将字符从十进制表示形式转换为十进制形式

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

在 BASH 中相互转换二进制、八进制、十进制和十六进制值
  1. 一些相关的Q&A:

在 BASH 中将十进制转换为十六进制。例子

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

在 BASH 中将十六进制转换为十进制。例子

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

在 BASH 中将十六进制转换为二进制。例子

echo "ibase=16; obase=2; FF" | bc
11111111
echo "obase=2; ibase=16; FF" | bc
11111111

在 BASH 中将二进制转换为十六进制。例子

echo "obase=16; ibase=2; 11111111" | bc
FF
echo "ibase=2; obase=16; 11111111" | bc # wrong
100110

在 BASH 中将十六进制转换为八进制。例子

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

在 BASH 中将八进制转换为十六进制。例子

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
© www.soinside.com 2019 - 2024. All rights reserved.