Solved

convert Hexadecimal file to decimal

Posted on 2000-03-26
19
555 Views
Last Modified: 2010-04-21
I have a big file with Hexadecimal numbers (one in every line) and i want to transplate the numbers to the decimal value.
0
Comment
Question by:yairt
  • 9
  • 2
  • 2
  • +6
19 Comments
 
LVL 12

Expert Comment

by:tel2
ID: 2657995
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
 

Author Comment

by:yairt
ID: 2658220
Adjusted points from 100 to 101
0
 

Author Comment

by:yairt
ID: 2658221
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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

 

Author Comment

by:yairt
ID: 2658250
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
 
LVL 2

Expert Comment

by:mapc
ID: 2658307
Use this:
perl -ne '$_=hex($_);printf("%c\n",$_);' < hexfile.txt
0
 

Author Comment

by:yairt
ID: 2658360
mapc, its not working.
0
 
LVL 2

Expert Comment

by:mapc
ID: 2658399
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
 

Author Comment

by:yairt
ID: 2658412
mapc, its not working.
0
 

Author Comment

by:yairt
ID: 2658424
Adjusted points from 101 to 103
0
 

Author Comment

by:yairt
ID: 2658425
I gut a lot of "0"s
Its still not good.

Yes I am from Israel
0
 

Author Comment

by:yairt
ID: 2658445
I gut a lot of "0"s
Its still not good.

Yes I am from Israel
0
 
LVL 40

Expert Comment

by:jlevie
ID: 2658551
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
 
LVL 84

Expert Comment

by:ozo
ID: 2658628
Can you show us what the file looks like?
0
 
LVL 2

Expert Comment

by:bedot
ID: 2661929
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
 

Expert Comment

by:ghins76
ID: 2668694
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
 
LVL 5

Expert Comment

by:ecw
ID: 2671479
perl -pe '/../ && ($_=hex()."\n")'
0
 
LVL 1

Accepted Solution

by:
sbuehrle earned 103 total points
ID: 2684695
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
 

Author Comment

by:yairt
ID: 2704434
thanks
0
 
LVL 84

Expert Comment

by:ozo
ID: 2709970
So what makes the accepted answer different from the other ones?
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In tuning file systems on the Solaris Operating System, changing some parameters of a file system usually destroys the data on it. For instance, changing the cache segment block size in the volume of a T3 requires that you delete the existing volu…
I have been running these systems for a few years now and I am just very happy with them.   I just wanted to share the manual that I have created for upgrades and other things.  Oooh yes! FreeBSD makes me happy (as a server), no maintenance and I al…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question