Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 05-31-2012
Registered User
 
Join Date: Jul 2011
Posts: 33
Thanks: 2
Thanked 0 Times in 0 Posts
How to convert a file containing hex code to decimal using script?

The file contains code like the below and need to convert each one into a decimal

00 00 00 04 17 03 06 01

So the output should come as

0 0 0 4 23 3 6 1
Sponsored Links
    #2  
Old 05-31-2012
jayan_jay's Avatar
Forum Advisor
 
Join Date: Jul 2008
Posts: 831
Thanks: 9
Thanked 185 Times in 176 Posts

Code:
$ for i in 00 00 00 04 17 03 06 01 ; do echo "ibase=16; $i" | bc ; done

Sponsored Links
    #3  
Old 05-31-2012
Scrutinizer's Avatar
Moderator
 
Join Date: Nov 2008
Location: Amsterdam
Posts: 7,345
Thanks: 144
Thanked 1,754 Times in 1,591 Posts
Without external programs

Code:
for i in 00 00 00 04 17 03 06 01 ; do
  printf "%d\n" "0x$i"
done

    #4  
Old 05-31-2012
Registered User
 
Join Date: Jul 2011
Posts: 33
Thanks: 2
Thanked 0 Times in 0 Posts
Hi Jayan

I have a file called hex.txt
the file contains numerous entries i, just gave 8 as an example , I need to pick up entire content from the file and convert to decimal and not just those 8 alone

Can u advise if that is possible
Sponsored Links
    #5  
Old 05-31-2012
Scrutinizer's Avatar
Moderator
 
Join Date: Nov 2008
Location: Amsterdam
Posts: 7,345
Thanks: 144
Thanked 1,754 Times in 1,591 Posts
Please post a representative sample of hex.txt and the desired output..
Sponsored Links
    #6  
Old 05-31-2012
Registered User
 
Join Date: Jul 2011
Posts: 33
Thanks: 2
Thanked 0 Times in 0 Posts
hex.txt contains numerous entries like this


Code:
00 00 00 04 0A 02 05 01 00 00 00 00 0A 0A 32 C7 00 00 00 00 0A 0A 32 F3 0A 0A 2B 2D 00 00 00 00 00 00 00 08 80 01 06 6D 26 E4 00 02 00 00 00 00
06 6D 0A 80 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

So for eg : 00 00 00 04 0A 02 05 01 should be converted into decimal form 0 0 0 4 10 2 5 1 and so on

Last edited by Franklin52; 05-31-2012 at 03:59 AM.. Reason: Please use code tags
Sponsored Links
    #7  
Old 05-31-2012
Scrutinizer's Avatar
Moderator
 
Join Date: Nov 2008
Location: Amsterdam
Posts: 7,345
Thanks: 144
Thanked 1,754 Times in 1,591 Posts
Try:

Code:
awk '{for(i=1;i<=NF;i++)$i=sprintf("%0d","0x"$i)}1' infile

Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Convert decimal notation to ANSI point code notation aavam Shell Programming and Scripting 4 06-28-2011 09:16 PM
To convert file with decimal to another file with Hexadecimal mathie Shell Programming and Scripting 10 06-08-2011 02:50 AM
Convert exponential value to decimal Sangtha Shell Programming and Scripting 5 07-23-2009 12:04 AM
PERL:How to convert numeric values txt file to PACKED DECIMAL File? aloktiwary Shell Programming and Scripting 1 05-20-2009 09:55 AM
having a bash script convert ft to meters with 1 decimal audiophile Shell Programming and Scripting 2 09-14-2008 10:00 AM



All times are GMT -4. The time now is 10:51 AM.