Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

Example: file1

 0x59999
 0x5acdc
 0xffeff

I want to start

$ cat file1 | util | cat >file2

and get file2 with smth like

 1021489
 1249230
 3458080

(numbers in example output are random, as I cant convert so long hex to dec)

Upd: perl : perl -pe '$_=hex;$_.="\n"'. Can anybody do it better?

The real task is a sorting of hex numbers.

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

3 Answers 3

up vote 2 down vote accepted

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.

share|improve this answer
    
it is hard to use with 100MB file of hex values - one value per line. –  osgx Dec 27 '10 at 21:35
add comment

I normally just use printf...

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

share|improve this answer
    
this will be done with bash builtins, w/o forks? –  osgx Dec 27 '10 at 21:34
add comment
cat file1 | wcalc -q -d -EE -P0 > file2

wcalc should be installed first

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.