Link to home
Start Free TrialLog in
Avatar of yairt
yairt

asked on

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.
Avatar of tel2
tel2
Flag of New Zealand image

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.
Avatar of yairt
yairt

ASKER

Adjusted points from 100 to 101
Avatar of yairt

ASKER

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.
Avatar of yairt

ASKER

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.
Use this:
perl -ne '$_=hex($_);printf("%c\n",$_);' < hexfile.txt
Avatar of yairt

ASKER

mapc, its not working.
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?
Avatar of yairt

ASKER

mapc, its not working.
Avatar of yairt

ASKER

Adjusted points from 101 to 103
Avatar of yairt

ASKER

I gut a lot of "0"s
Its still not good.

Yes I am from Israel
Avatar of yairt

ASKER

I gut a lot of "0"s
Its still not good.

Yes I am from Israel
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";
}
Can you show us what the file looks like?
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

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;
}  
/**************************************************/
perl -pe '/../ && ($_=hex()."\n")'
ASKER CERTIFIED SOLUTION
Avatar of sbuehrle
sbuehrle

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of yairt

ASKER

thanks
So what makes the accepted answer different from the other ones?