Linux – Convert a list of decimal values in a text file into hex format

conversionlinuxnumeric datapythonscripting

I have a need to convert a list of decimal values in a text file into hex format, so for example test.txt might contain:

131072
196608
262144
327680
393216
...

the output should be list of hex values (hex 8 digit, with leading zeroes):

00020000
00030000
00040000
...

the output is printed into the text file. How to make this with python or linux shell script?

EDIT #1

I missed one extra operation: I need to add 80000000 hex to each of the created hex values. (arithmetic addition, to apply to already created list of hex values).

Best Answer

You can do this using printf and bash:

printf '%08x\n' $(< test.txt)

Or using printf and bc...just...because?

printf '%08s\n' $(bc <<<"obase=16; $(< test.txt)")

In order to print the output to a text file just use the shell redirect > like:

printf '%08x\n' $(< test.txt) > output.txt
Related Question