Domanda

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?

È stato utile?

Soluzione

Use format specifiers:

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

$ printf "%04X\n" 1
0001
$ printf "%04X\n" 42
002A
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top