Convert decimal to hex in Bash?

A quicky for my archives: Convert decimal to hex in Bash, and hexadecimal to decimal. 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…

Convert decimal to hex in bash

You can use the Linux bash shell to convert decimal values to hexadecimal, and vice versa. For example:

Decimal to hexadecimal in bash

printf '%x\n' 26560

Hexadecimal to decimal

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

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'.

Did this post helped you solve a problem? Or dit you find it interesting? Support Sysadmins of the North with a direct donation via Paypal of by bank wire transfer IBAN: NL31 ABNA 0432217258 (Jan Reilink). Just $ 5,- or € 5,- is more than enough, thanks!

Read this too:   How to delete all MAILER-DAEMON emails in Postfix queue



Leave a Reply

Your email address will not be published. Required fields are marked *