Convert decimal to hex in Bash?

A quicky for my archives: convert decimal to hex in Bash, and convert hexadecimal to decimal, in Bash. In mathematics and computing, hexadecimal (also base 16, or hex) is a positional numeral system with a radix, or base, of 16. It uses sixteen distinct symbols, most often the symbols 0-9 to represent values zero to nine, and A, B, C, D, E, F (or alternatively a-f) to represent values ten to fifteen. If you want to convert hexadecimal values to decimal and decimal values to hexadecimal, here’s how. All on the bash prompt…

You can use your Linux Bash shell to easily convert decimal values to hexadecimal, and vice versa. For example:

Decimal to hexadecimal in bash

Convert decimal to hexadecimal in Bash:

printf '%x\n' 26560
67c0Code language: Bash (bash)

Hexadecimal to decimal

Convert hexadecimal to decimal in Bash:

echo $((0x000067c0))
26560Code language: Bash (bash)
echo $((0x67c0))
26560Code language: Bash (bash)
printf '%d\n' 0x67c0
26560Code language: Bash (bash)

Convert Unix timestamps to dates in Bash

How to convert Unix timestamps to dates in Bash is a little irrelevant in a post on converting decimal to hex in Bash, but it’s still converting something in Bash :-) .

Suppose you have an Unix timestamp that you want to convert to a date, here is how with two one liners:

date -d @timestamp
Code language: Bash (bash)
date -d @($date -u +timestamp)
Code language: Bash (bash)

And vice versa, convert a date to an Unix timestamp:

date -d '04/05/2017 11:13:00' +"%s"
Code language: Bash (bash)

The date string is too complex to be documented in the man page, so it is described in info: info '(coreutils) date invocation'.

Jan Reilink

Hi, my name is Jan. I am not a hacker, coder, developer or guru. I am merely an application manager / systems administrator, doing my daily thing at Embrace - The Human Cloud. In the past I worked for clidn and Vevida. With over 20 years of experience, my specialties include Windows Server, IIS, Linux (CentOS, Debian), security, PHP, websites & optimization. I blog at https://www.saotn.org.