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…

Convert decimal to hex in bash

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

Convert decimal to hexadecimal in Bash:

```.wp-block-code {
border: 0;
}

.wp-block-code > div {
overflow: auto;
}

.shcb-language {
border: 0;
clip: rect(1px, 1px, 1px, 1px);
-webkit-clip-path: inset(50%);
clip-path: inset(50%);
height: 1px;
margin: -1px;
overflow: hidden;
position: absolute;
width: 1px;
word-wrap: normal;
word-break: normal;
}

.hljs {
box-sizing: border-box;
}

.hljs.shcb-code-table {
display: table;
width: 100%;
}

.hljs.shcb-code-table > .shcb-loc {
color: inherit;
display: table-row;
width: 100%;
}

.hljs.shcb-code-table .shcb-loc > span {
display: table-cell;
}

.wp-block-code code.hljs:not(.shcb-wrap-lines) {
white-space: pre;
}

.wp-block-code code.hljs.shcb-wrap-lines {
white-space: pre-wrap;
}

.hljs.shcb-line-numbers {
border-spacing: 0;
counter-reset: line;
}

.hljs.shcb-line-numbers > .shcb-loc {
counter-increment: line;
}

.hljs.shcb-line-numbers .shcb-loc > span {
}

.hljs.shcb-line-numbers .shcb-loc::before {
border-right: 1px solid #ddd;
content: counter(line);
display: table-cell;
text-align: right;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
white-space: nowrap;
width: 1%;
}
```printf '%x\n' 26560
67c0```Code language: Bash (bash)```

Convert hexadecimal to decimal in Bash:

``````echo \$((0x000067c0))
26560```Code language: Bash (bash)```
``````echo \$((0x67c0))
26560```Code language: Bash (bash)```
``````printf '%d\n' 0x67c0
26560```Code 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'`.

Sharing is caring