convert Hexadecimal file to decimal

I have a big file with Hexadecimal numbers (one in every line) and i want to transplate the numbers to the decimal value.
yairtAsked:
Who is Participating?
 
sbuehrleConnect With a Mentor Commented:
You can do it with a script like this. Just replace "hex.txt" with the name of
your file that contains hex numbers.  Make sure bc is in your path, ortherwise give the complete path to bc.


#!/bin/sh
while read hex
do
hex=`echo $hex |tr [a-z] [A-Z]`
bc<<EOF
ibase=16
obase=A
$hex
EOF
done<hex.txt
0
 
tel2Commented:
yairt,

check out the man pages for the "od" (Octal Dump) standard UNIX utility.  In addition to octal, it can do Hex, etc.

Let me know if you have any problems, and I'll give you more info like switches, etc.
0
 
yairtAuthor Commented:
Adjusted points from 100 to 101
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
yairtAuthor Commented:
As I understood the od/xd its convert each char including spaces and every digit by it self.
I need to convert a Hex NUMBER (few digits) to Dec.
0
 
yairtAuthor Commented:
As I understood the od/xd its convert each char including spaces and every digit by it self.
I need to convert a Hex NUMBER (few digits) to Dec.
0
 
mapcCommented:
Use this:
perl -ne '$_=hex($_);printf("%c\n",$_);' < hexfile.txt
0
 
yairtAuthor Commented:
mapc, its not working.
0
 
mapcCommented:
ah.. you wanted DECIMAL value.
use printf("%d") and not %c then.
And won't you tell us what is happening.
are you from Israel?
0
 
yairtAuthor Commented:
mapc, its not working.
0
 
yairtAuthor Commented:
Adjusted points from 101 to 103
0
 
yairtAuthor Commented:
I gut a lot of "0"s
Its still not good.

Yes I am from Israel
0
 
yairtAuthor Commented:
I gut a lot of "0"s
Its still not good.

Yes I am from Israel
0
 
jlevieCommented:
Try the following, it will handle cases with leading or trailing spaces, and the presence of a leading 0x or 0X. Save it to a file (to-dec), make the file executable (chmod +x to-dec) and use it like "to-dec <hex-file >decimal-file"

#!/usr/bin/perl
#
while(<STDIN>)
{
  $_ =~ s/\s+//g;
  $_ =~ s/^0[xX]//;
  $dec = hex;
  print "$dec\n";
}
0
 
ozoCommented:
Can you show us what the file looks like?
0
 
bedotCommented:
you must explain haw long the hexa word you want to convert in decimal

od permit only conversion of byte

more efficient should be a compiled C program

0
 
ghins76Commented:
Try the following C program. Let the file name be the first argument to the program.

/***********************************************/
#include <stdio.h>

int main(int argc, char **argv)
{
        int h;
        FILE *fp = fopen(argv[1],"r");

        while(!feof(fp))
        {
                fscanf(fp,"%x", &h);
                printf("\n %d \n",h);
        }
        return 0;
}  
/**************************************************/
0
 
ecwCommented:
perl -pe '/../ && ($_=hex()."\n")'
0
 
yairtAuthor Commented:
thanks
0
 
ozoCommented:
So what makes the accepted answer different from the other ones?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.