Solved

convert Hexadecimal file to decimal

Posted on 2000-03-26
19
551 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

A metadevice consists of one or more devices (slices). It can be expanded by adding slices. Then, it can be grown to fill a larger space while the file system is in use. However, not all UNIX file systems (UFS) can be expanded this way. The conca…
Java performance on Solaris - Managing CPUs There are various resource controls in operating system which directly/indirectly influence the performance of application. one of the most important resource controls is "CPU".   In a multithreaded…
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.:
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

810 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