7

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.

2
  • Neither of the cat commands in your hypothetical pipeline should be necessary. Dec 28, 2010 at 2:04
  • @Dennis Williamson, yes, they are. But I want to show, that this command is used in the pipe/
    – osgx
    Dec 28, 2010 at 9:29

4 Answers 4

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.

1
  • it is hard to use with 100MB file of hex values - one value per line.
    – osgx
    Dec 27, 2010 at 21:35
4

I normally just use printf...

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

1
  • this will be done with bash builtins, w/o forks?
    – osgx
    Dec 27, 2010 at 21:34
0
cat file1 | wcalc -q -d -EE -P0 > file2

wcalc should be installed first

5
  • but perl is installed on many.
    – osgx
    Dec 27, 2010 at 18:28
  • $ wcalc bash: wcalc: command not found
    – osgx
    Dec 27, 2010 at 18:29
  • osgx: if you wanted Perl solutions you should have asked on stackoverflow. This is not a programming website.
    – user1931
    Dec 27, 2010 at 19:05
  • Little bit of UUOC, too :p
    – user1931
    Dec 27, 2010 at 20:31
  • I want a shell solution. AWK, SED can be used. PERL one-liner is in my question already ))
    – osgx
    Dec 27, 2010 at 21:36
0

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

DB50BA84BAEA0BDE2174F1607347C77337498EB90A7C1A5C4860C42A0E84D510A6437A69E7F48421BA05ADD2ED1EAD17F60CC75C12D7CEA73137BC0408AF0242E37E7EE6A4A753C01534EC7C9C9F3156FCA9D3E93FE9963E58C1A1974E70D8D287E4D279F4E3B52048332FF4B63F52ACA85A1B9BE42016C4E57FF015E0C29E9F

Command:

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

Result:

154008473420712387404901698031482866054500588587768119192674838113184401106477187614030931854609850184392137108127027338775356787883074794696991150075835606063573780179056429457464518736685347520207122186156775440321726025030196545084526749015303353858750937930411164298939052722221299315903418569137408089759

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .