Solved

convert Hexadecimal file to decimal

Posted on 2000-03-26
19
557 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Shell script errors 10 143
aix tls version 6 558
replace column/record content in a *csv file based on a regular expression 6 81
Using sort and uniq to pare down large syslog 6 78
This tech tip describes how to install the Solaris Operating System from a tape backup that was created using the Solaris flash archive utility. I have used this procedure on the Solaris 8 and 9 OS, and it shoudl also work well on the Solaris 10 rel…
FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
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…

696 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