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?

Était-ce utile?

La solution

Use format specifiers:

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

$ printf "%04X\n" 1
0001
$ printf "%04X\n" 42
002A
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top