How to convert a file containing hex code to decimal using script?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to convert a file containing hex code to decimal using script?
# 8  
Old 05-31-2012
The conversion didnt occur

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


Last edited by Scrutinizer; 05-31-2012 at 04:31 AM.. Reason: code tags
# 9  
Old 05-31-2012
Try with this ..
Code:
$ for i in $(cat infile) ; do echo "ibase=16; $i" | bc ; done | xargs

This User Gave Thanks to jayan_jay For This Post:
# 10  
Old 05-31-2012
What do you get when you run it on your own sample?
I get:
Code:
$ awk '{for(i=1;i<=NF;i++)$i=sprintf("%0d","0x"$i)}1' infile
0 0 0 4 10 2 5 1 0 0 0 0 10 10 50 199 0 0 0 0 10 10 50 243 10 10 43 45 0 0 0 0 0 0 0 8 128 1 6 109 38 228 0 2 0 0 0 0
6 109 10 128 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

What OS and version are you using?
# 11  
Old 05-31-2012
Quote:
Originally Posted by jayan_jay
Try with this ..
Code:
$ for i in $(cat infile) ; do echo "ibase=16; $i" | bc ; done | xargs

Thanks Jayan , that worked

Just need to format it now .I will get back on this

Scrutinizer , havent checked the OS yet
# 12  
Old 05-31-2012
If using gawk, then try option --non-decimal-data
Code:
$ awk '{for(i=1;i<=NF;i++)$i=sprintf("%0d","0x"$i)}1' infile
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

$ awk --non-decimal-data '{for(i=1;i<=NF;i++)$i=sprintf("%0d","0x"$i)}1' infile
0 0 0 4 10 2 5 1 0 0 0 0 10 10 50 199 0 0 0 0 10 10 50 243 10 10 43 45 0 0 0 0 0 0 0 8 128 1 6 109 38 228 0 2 0 0 0 0
6 109 10 128 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

$ awk --version
GNU Awk 3.1.7

This User Gave Thanks to Ygor For This Post:
# 13  
Old 05-31-2012
Thanks Ygor, that solves the mystery. Another gawk quirk, it seems..
Code:
awk --posix

also seems to do the trick, perhaps for different reasons? It looks like gawk (as an extension) is interpreting these constants, while reading them, whereas I want them to be treated as simple strings and let printf do the conversion...

Last edited by Scrutinizer; 05-31-2012 at 07:13 AM..
# 14  
Old 05-31-2012
I have written the data into hexdata.log output as for one of the entries

Code:
0 0 0 4 235 20 192 65 0 0 0 0 10 20 40 99 10 10 34 24 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 152 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

Need to be able to pull out words , these are actually IPs
5-8 -235.20.192.65
13-16 -10.20.40.99
16-20 - 10.10.34.24
29-33 - 0 ( C -Count )

So the output file reads as

Code:
245.20.192.65 10.20.40.99 10.10.34.24 c:0

Moderator's Comments:
Mod Comment Please view this link to learn the use of code tags.

Last edited by Scrutinizer; 05-31-2012 at 08:41 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Print hex Ip address in decimal format inside awk script

Hi to all, May someone help me with the following. The awk script below is part of a bigger awk script, and this part attempts to print an Ip address that is in hex format in decimal format. I'm trying with following code but I'm getting 0.0.0.0 and the correct answer is 192.168.140.100 ... (9 Replies)
Discussion started by: Ophiuchus
9 Replies

2. Shell Programming and Scripting

Convert Binary File To Hex In Linux

dHi, I have the attached file(actual file can be extracted post unzipping it) & i am trying to use the following code for coversion to hex format. Starting hex value is 84 which is start of the record & termination is done using 00 00 followed by 84(hex) which i can see in the dump clearly using... (14 Replies)
Discussion started by: siramitsharma
14 Replies

3. Programming

Urgent help needed.. C++ program to convert decimal to hexa decimal

Hi , seq can be 0...128 int windex = seq / 8; int bindex = seq % 8; unsigned char bitvalue = '\x01' << (7-bindex) ; bpv.bitmapvalue = bitvalue; This is the part of a program to convert decimal to bitmap value of hexadecimal. I want this to change to convert only to... (1 Reply)
Discussion started by: greenworld123
1 Replies

4. Shell Programming and Scripting

Convert hex to decimal

can someone help me in converting hex streams to decimal values using perl script Hex value: $my_hex_stream="0c07ac14001676"; Every hex value in the above stream should be converted in to decimal and separated by comma. The output should be: 12,07,172,20,00,22,118 (2 Replies)
Discussion started by: Arun_Linux
2 Replies

5. UNIX for Advanced & Expert Users

Convert 32 bit hex value into fields in decimal

I have 32 bit value in hex that I want to separate into fields and then convert the fields into decimal values. Input file has 2 words of 32 bit hex values: 000001ac ca85210e Output both words separated into individual bit fields: ca85210e: f1(31:9), f2(8:0) f7c392ac: f1(31:14),... (2 Replies)
Discussion started by: morrbie
2 Replies

6. Shell Programming and Scripting

Decimal to hex conversion

Dear All PROs Thanks in advance need a shell for Decimal to hex conversion input file (decimal values) 65,5,48,66,133,131,118,47 65,5,48,66,133,131,83,63 . . desire output should be (Hex value)... (11 Replies)
Discussion started by: The_Archer
11 Replies

7. Shell Programming and Scripting

Convert hex to decimal or reverse is better?

Please Help Me! about the problem down under. I have 2 files with nearly the same characteristics, I have to convert one to the other format or the other format to one's format. I want to write it with awk. The first file contain lines like this: 300000001#A#Y#Y#Y#Y The other file contain... (4 Replies)
Discussion started by: Axel82
4 Replies

8. Shell Programming and Scripting

How to convert hex numbers to decimal ?

Hi, please tell me how to convert hex number to decimal 000000E7 000000000002640D 0000000000025B16 and seconds to minutes, hours, days, months, years bytes to kbytes, mbytes , gbytes read the following examples while read a b do printf "%5d %5d\n" "0x$a" "0x$b" done < "$FILE"... (15 Replies)
Discussion started by: jack2
15 Replies

9. Shell Programming and Scripting

having a bash script convert ft to meters with 1 decimal

What is the correct syntax to limit the number of decimals to 1 or 0 in a bash script? Here is the partial code I have which works, but if I echo $meters, it has 4 or 5 decimals: METERS=`echo "$FEET * 0.3048" | bc` I read about scale and length in the bc man page, but I can't seem to get the... (2 Replies)
Discussion started by: audiophile
2 Replies

10. Shell Programming and Scripting

hex to decimal

hi all, echo "ibase=16;obase=10;11" | bc shouldn't i get 17? i am getting 11 i am trying to convert 11 (hex) to decimal stuck! JAK (4 Replies)
Discussion started by: jakSun8
4 Replies
Login or Register to Ask a Question