Linux shell utils: convert a list of hexadecimals to a list of decimals

7

6

How can I convert a file with a lot of hex numbers into decimal?

Example: file1

 0x59999
 0x5acdc
 0xffeff

I want to start

$ cat file1 | util | cat >file2

and get file2 with something like:

 1021489
 1249230
 3458080

(The numbers in example output are random, as I can't convert such long hex numbers to decimal)

UPDATE: I solved this with the following Perl command:

perl -pe '$_=hex;$_.="\n"'

Is there a better solution? The real task is needing to be able to sort hex numbers.

osgx

Posted 2010-12-27T16:08:47.030

Reputation: 5 419

Neither of the cat commands in your hypothetical pipeline should be necessary. – Paused until further notice. – 2010-12-28T02:04:07.257

@Dennis Williamson, yes, they are. But I want to show, that this command is used in the pipe/ – osgx – 2010-12-28T09:29:44.840

Answers

7

John T's answer is the way to go for hex conversions, but you can also do them this way (which can be used for other bases as well):

$ hexval=0x59999
$ hexval=${hexval#*x}
$ echo $((16#$hexval))
367001

Demonstration:

$ echo $((2#1011010))
90
$ echo $((8#1776))
1022
$ echo $((23#mmmmm))
6436342

Edit:

#!/bin/bash
base=16
while read -r val
do
    val=${val#*x}
    echo $(($base#$val))
done < inputfile > outputfile

The only advantage over John T's answer is that this one can be easily adapted to convert other bases to decimal. His is quite a bit faster.

This gawk command seems to be a little faster than John's shell version:

gawk --non-decimal-data '{printf "%d\n",$1}' inputfile > outputfile

It's about the same speed as your Perl command. Why not just use it?

By the way, the last part of your Perl one-liner can be replaced by the -l option:

perl -lpe '$_=hex'

Another note: Typically the pipeline you show in your question would be written as:

util < file1 > file2

or, if cat represents placeholders, then dummy names should be used:

prog1 < file1 | util | prog2 > file2

then you won't have people complaining about useless uses of cat.

Paused until further notice.

Posted 2010-12-27T16:08:47.030

Reputation: 86 075

it is hard to use with 100MB file of hex values - one value per line. – osgx – 2010-12-27T21:35:18.000

4

I normally just use printf...

while read x;do printf '%d\n' $x;done <file

John T

Posted 2010-12-27T16:08:47.030

Reputation: 149 037

this will be done with bash builtins, w/o forks? – osgx – 2010-12-27T21:34:40.700

0

Only Python worked for me for longer hex values such as:

DB50BA84BAEA0BDE2174F1607347C77337498EB90A7C1A5C4860C42A0E84D510A6437A69E7F48421BA05ADD2ED1EAD17F60CC75C12D7CEA73137BC0408AF0242E37E7EE6A4A753C01534EC7C9C9F3156FCA9D3E93FE9963E58C1A1974E70D8D287E4D279F4E3B52048332FF4B63F52ACA85A1B9BE42016C4E57FF015E0C29E9F

Command:

python -c 'print(int("DB50BA84BAEA0BDE2174F1607347C77337498EB90A7C1A5C4860C42A0E84D510A6437A69E7F48421BA05ADD2ED1EAD17F60CC75C12D7CEA73137BC0408AF0242E37E7EE6A4A753C01534EC7C9C9F3156FCA9D3E93FE9963E58C1A1974E70D8D287E4D279F4E3B52048332FF4B63F52ACA85A1B9BE42016C4E57FF015E0C29E9F", 16))'

Result:

154008473420712387404901698031482866054500588587768119192674838113184401106477187614030931854609850184392137108127027338775356787883074794696991150075835606063573780179056429457464518736685347520207122186156775440321726025030196545084526749015303353858750937930411164298939052722221299315903418569137408089759

Cyborg

Posted 2010-12-27T16:08:47.030

Reputation: 121

0

cat file1 | wcalc -q -d -EE -P0 > file2

wcalc should be installed first

jet

Posted 2010-12-27T16:08:47.030

Reputation: 2 675

but perl is installed on many. – osgx – 2010-12-27T18:28:50.600

$ wcalc bash: wcalc: command not found – osgx – 2010-12-27T18:29:12.597

osgx: if you wanted Perl solutions you should have asked on stackoverflow. This is not a programming website. – John T – 2010-12-27T19:05:15.840

Little bit of UUOC, too :p – John T – 2010-12-27T20:31:51.213

I want a shell solution. AWK, SED can be used. PERL one-liner is in my question already )) – osgx – 2010-12-27T21:36:03.490