Hexadecimal to decimal or vice versa with printf

From Brandonhutchinson.com

Jump to: navigation, search
  • Decimal to hexadecimal:
$ printf "%02X\n" decimal number
  • Hexadecimal to decimal:
$ printf "%d\n" hexadecimal number

Examples

  • Convert 1024 to hexadecimal.
$ printf "%02X\n" 1024
400
  • Convert 0x400 to decimal.
$ printf "%d\n" 0x400
1024
Personal tools