Convert decimal to hex in Bash, hexadecimal to decimal and dates to Unix timestamp

Home » GNU/Linux » Convert decimal to hex in Bash, hexadecimal to decimal and dates to Unix timestamp

A quicky for my archives, learn how to convert decimal to hex and hexadecimal to decimal, in Bash…

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

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 command-prompt…

For example:

Decimal to hexadecimal in bash

Convert decimal to hexadecimal in Bash:

printf '%x\n' 26560
67c0

Hexadecimal to decimal

Convert hexadecimal to decimal in Bash:

echo $((0x000067c0))
26560
echo $((0x67c0))
26560
printf '%d\n' 0x67c0
26560

Bash function to generate a random alphanumeric string

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
date -d @($date -u +timestamp)

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

date -d '04/05/2017 11:13:00' +"%s"

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

Summary

  • You can easily convert decimal to hex and hexadecimal to decimal using your Linux Bash shell.
  • Hexadecimal uses symbols 0-9 and A-F to represent values, making it a base 16 numeral system.
  • The article provides methods for converting decimal to hexadecimal and vice versa using Bash commands.
  • It also briefly discusses converting Unix timestamps to dates, although it’s less relevant to the main topic.
Jan Reilink
Jan Reilink

In my day to day work, I’m a systems administrator – DevOps / SRE and applications manager at Embrace – The Human Cloud. At Embrace we develop, maintain and host social intranets for our clients. Provide digital services and make working more efficient within various sectors.

Articles: 154