skymario84
skymario84

Reputation: 123

DEC to HEX conversion with filling empty with 0 in bash

I have to convert decimal number to hexadecimal, but with filling eventual voids with zeroes:

Example:

I've tried this:

printf "%X\n" 190

Output is:

BE

i need it to look like this:

00BE

In short, output should have 4 hex symbols, if less, it should be filled with zeroes at the beginning

How to do that in bash?

Upvotes: 0

Views: 154

Answers (1)

devnull
devnull

Reputation: 123528

Use format specifiers:

$ printf "%04X\n" 190
00BE

$ printf "%04X\n" 1
0001
$ printf "%04X\n" 42
002A

Upvotes: 3

Related Questions