SoFunction
Updated on 2025-03-04

How to convert decimal to hexadecimal in Linux printf

Linux printf converts decimal to hexadecimal

During the development and troubleshooting process, sometimes we need to do some basic conversion. Here are some quick tips:

The following is

Convert decimal to hexadecimal

❯ printf  "0x%x\n" 100
0x64

Can also

Convert hexadecimal to decimal

❯ printf "%d\n" 0x64
100

In fact, %x is the format to be converted in the quotes, \n is the new line "new line"

Well enough

Convert decimal to octal

❯ printf "%o\n" 100
144

In addition, using the bc command for conversion is also possible. In most Linux distributions, the bc tool is already preinstalled.

Convert decimal to hexadecimal

❯ echo "obase=16; 100" | bc
64

Convert hexadecimal to decimal

❯ echo "ibase=16; 64" | bc
100

Convert decimal to octal

❯ echo "obase=8; 100" | bc
144

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.