Question

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?

Was it helpful?

Solution

Use format specifiers:

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

$ printf "%04X\n" 1
0001
$ printf "%04X\n" 42
002A
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top