Solved

convert Hexadecimal file to decimal

Posted on 2000-03-26
19
545 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 11

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
 

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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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…
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…
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…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

708 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now