[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

convert Hexadecimal file to decimal

Posted on 2000-03-26
19
Medium Priority
?
574 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
Industry Leaders: 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 309 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

Installing FreeBSD… FreeBSD is a darling of an operating system. The stability and usability make it a clear choice for servers and desktops (for the cunning). Savvy?  The Ports collection makes available every popular FOSS application and packag…
Using libpcap/Jpcap to capture and send packets on Solaris version (10/11) Library used: 1.      Libpcap (http://www.tcpdump.org) Version 1.2 2.      Jpcap(http://netresearch.ics.uci.edu/kfujii/Jpcap/doc/index.html) Version 0.6 Prerequisite: 1.      GCC …
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 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…
Suggested Courses

649 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