How to convert a character from and to its decimal, binary, octal, or hexadecimal representations in BASH / Shell ?
Asked
Active
Viewed 650 times
1 Answers
4
Convert a character from and to its decimal, binary, octal, or hexadecimal representations in BASH with printf
and od
- Some relevant documentation and Q&A:
od
man pages:printf
man pages:- What is the difference between UTF-8 and Unicode?
- How do I print an ASCII character by different code points in Bash?
- How to print an octal value's corresponding UTF-8 character in bash?
- Unicode char representations in BASH / shell: printf vs od
Convert a character from and to its decimal representation
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
Convert a character from and to its binary representation
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
Convert a character from and to its octal representation
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"
Convert a character from and to its hexadecimal representation
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"
Convert binary, octal, decimal and hexadecimal values between each other in BASH with bc
and printf
- Some relevant Q&A:
- Understand "ibase" and "obase" in case of conversions with bc?
- TL;DR:
ibase
andobase
params order matters, but not always. Hex values must be in UPPERCASE.
- TL;DR:
Convert decimal to hexadecimal in BASH. Examples
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
Convert hexadecimal to decimal in BASH. Examples
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
Convert hexadecimal to binary in BASH. Examples
echo "ibase=16; obase=2; FF" | bc
11111111
echo "obase=2; ibase=16; FF" | bc
11111111
Convert binary to hexadecimal in BASH. Examples
echo "obase=16; ibase=2; 11111111" | bc
FF
echo "ibase=2; obase=16; 11111111" | bc # wrong
100110
Convert hexadecimal to octal in BASH. Examples
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
Convert octal to hexadecimal in BASH. Examples
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

KiriSakow
- 957
- 1
- 12
- 22