Convert Binary File To Hex In Linux


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Convert Binary File To Hex In Linux
# 8  
Old 09-07-2015
What exactly are you trying to achieve?
# 9  
Old 09-07-2015
Hi RudiC,
Trying to write the required output(sequence no & timestamp) from hex converted file(DDI15.09.02.C_HEX) from original binary file(DDI15.09.02.C) attached earlier.
# 10  
Old 09-07-2015
As long as you keep us guessing (e.g. on you file structrue) and don't post exact specifications, I'm afraid we (at least I) can't help further.
This User Gave Thanks to RudiC For This Post:
# 11  
Old 09-07-2015
Ok..let me iterate whole things

I have changed the binary file to hex using the following code.


Code:
hexdump -v -e '1/1 "%02x "' DDI15.09.02.C|sed 's/\(00 00\) \(84\)/\1\n\2/g' >DDI15.09.02.C_HEX

Fileformat of DDI15.09.02.C_HEX is as follows:
Code:
84 7d 00 0c 09 40 11 8a 14 93 51 51 72 74 0f 09 02 0f 05 26 0d 65 06 51 55 21 67 01 00 00 6e 06 8a 14 93 51 51 72 98 09 03 04 46 04 04 32 04 9a 07 01 05 00 20 00 a8 08 01 10 06 51 55 21 c6 0f 01 05 b0 00 00 02 08 29 00 00 a9 00 00 cc 18 01 03 01 03 04 92 18 04 04 92 18 05 08 02 92 18 00 01 47 06 03 01 96 08 af 00 cf 14 00 00 
84 00 0c 08 40 11 6a 14 15 10 77 60 74 0f 09 02 0f 03 01 01 65 0a 94 14 10 92 15 67 03 00 00 6c 4f 56 4f 44 41 4c 07 00 09 6e 06 4f b1 85 aa 7a 2a 00 00 8e 0a 03 13 6a 14 15 10 77 60 a8 0a 01 10 0a 94 14 10 92 15 c6 0f 01 05 d8 06 00 02 08 26 00 00 80 00 00 cc 17 01 03 01 02 03 30 03 04 4b 18 05 08 02 4b 18 00 00 26 06 03 01 96 08 b0 00 cf 14 00 00

Now from the above hex files i want to fetch data from 9th fields onwards which is covered in function ano(i) in the code below & from 15th onwards for the timestamp which is written in function timestamp. But i am getting the required output via function ano but not from timestamp function which is coming as 0 .
Hope it clears the requirement

Code:
awk 'function ano(i)
{
$str=sprintf("%d%d%d%d%d",$i,$(i+1),$(i+2),$(i+3),$(i+4),$(i+5),$(i+6))
return $str
}
function timestamp(j)
{
$s=sprintf("%02d/%02d/%02d %02d:%02d:%02d",strtonum("0x"$j),strtonum("0x"$(j+1)),strtonum("0x"$(j+2)),strtonum("0x"$(j+3)),strtonum("0x"$(j+4)),strtonum("0x"$(j+5)),strtonum("0x"$(j+6)))
return $s
}
{
print ano(9),timestamp(15)
} ' DDI15.09.02.C_HEX

# 12  
Old 09-07-2015
Hi siramitsharma...

I have expanded your zip file and the start bytes are nothing like yours, that helps - not.

So $9, $10, $11, $12 and $13 are BCD AND $15, $16, $17 are HEX values for the year in the file you gave; that is 15\09\03 ; and similarly $18, $19, $20 for the time...

Why did you not tell us that part of the info is BCD and the other part HEX to decimal conversion?

Longhand the second part only for just ONE set of values and added a newline just to make it clear...
The BCD part is easy...
OSX 10.7.5, default bash terminal...
Code:
Last login: Mon Sep  7 13:58:50 on ttys000
AMIGA:barrywalker~> IFS=" "
AMIGA:barrywalker~> ARRAY=( $( cat /tmp/hexstring ) )
AMIGA:barrywalker~> printf "%02d\%02d\%02d %02d:%02d:%02d\n" 0x${ARRAY[14]} 0x${ARRAY[15]} 0x${ARRAY[16]} 0x${ARRAY[17]} 0x${ARRAY[18]} 0x${ARRAY[19]}
15\09\03 18:43:11
AMIGA:barrywalker~> _

# 13  
Old 09-07-2015
Thanks Wisecracker & RudiC for the help & apologies for the info not shared earlier. Thought that i would be able to do it.
Anyways, can you please suggest some other alternative looking into my requirement? Thanks in advance
# 14  
Old 09-07-2015
Please take way more care when specifiying your request! Some comments:
a) the two lines in your sample file in post#11 don't have the same structure, so anything developed for one line will fail on the other.
b) in your "ano" function you use byte 9 up to 9+6=15, in "timestamp" you use bytes 15+. There's an overlap; byte 15 is used/interpreted/worked upon twice; it's beyond my imagination that this be correct.
c) you didn't spend a second to explain the structure of the file; wisecracker (as well as others like me) did a good job in guessing/determining it but this is certainly part of the specification and not in the role of the coder.
d) the strtonum function is not offered by ALL awk versions, and I think you only need it in very special cases due to awk's variables' string/number dualism.

---------- Post updated at 20:52 ---------- Previous update was at 18:18 ----------

Howsoever, the main problems in your awk script are the assignments to $str and $s. As neither of those is defined, they evaluate to "" or 0 , and thus the assignments overwrite $0 i.e. the source line worked upon. That's why after the call to "ano", "timestamp" has nothing to read anymore.
Try
Code:
awk '
function ano(i)
        {return sprintf("%02d%02d%02d%02d%02d%02d",$i,$(i+1),$(i+2),$(i+3),$(i+4),$(i+5))
        }
function timestamp(j)
        {return sprintf("%02d/%02d/%02d %02d:%02d:%02d", "0x"$j,"0x"$(j+1),"0x"$(j+2),"0x"$(j+3),"0x"$(j+4),"0x"$(j+5),"0x"$(j+6))
        }

        {print ano(9),timestamp(15)
        }
' file
149351517274 15/09/02 15:05:38
151077607400 09/02/15 03:01:01
141510776074 15/09/02 15:03:01

The second line is from your sample file, the third after correction of the byte sequence by inserting a 0X7D in the second position of the line.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Want to convert an expect script to binary in Linux

Does anyone know how to convert an expect script to binary in linux?. (3 Replies)
Discussion started by: John Wilson
3 Replies

2. Shell Programming and Scripting

Convert jpg file to binary format

Hi Team, Here's the requirement. I have a image file in jpg format in unix. Now I need to i. convert the jpg format to binary format ii. followed by loading the binary file to Oracle db. Can anyone help me out? Thanks Krishnakanth Manivannan (4 Replies)
Discussion started by: kmanivan82
4 Replies

3. Shell Programming and Scripting

Convert binary file to csv and then back to the binary format

Hello *nix specialists, Im working for a non profit organisation in Germany to transport DSL over WLAN to people in areas without no DSL. We are using Linksys WRT 54 router with DD-WRT firmware There are at the moment over 180 router running but we have to change some settings next time. So my... (7 Replies)
Discussion started by: digidax
7 Replies

4. Shell Programming and Scripting

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 (24 Replies)
Discussion started by: necro98
24 Replies

5. Shell Programming and Scripting

convert hex to binary and send to variable

Folks, can anyone help with a script to convert hex to binary digits, and break the 32 bit binary into packs of 4 and send them to 8 different variables.Any help is sincerely appreciated. Thanks venu Its in korn shell...... (24 Replies)
Discussion started by: venu
24 Replies

6. UNIX for Dummies Questions & Answers

convert a .tr file to binary in cygwin

I would like to convert a .tr file to a binary file to use for trace purposes. Can anyone provide any insight? (0 Replies)
Discussion started by: sparklezilla3
0 Replies

7. Shell Programming and Scripting

binary/hex output

i want to output something like 2f 00 00 00 but i can't seem to escape characters like i'm used to in some programming languages, so with this: echo "/\0\0\0" >> outputfile i actually get 2f 5c 30 5c 30 5c 30 0a ie the \0 isn't giving me the 00 i need, and in addition it has got an... (8 Replies)
Discussion started by: peterworth
8 Replies

8. UNIX for Dummies Questions & Answers

How to convert binary Unix file to text

Hi all, I have a print control file (dflt) for Oracle which is in binary. As I am going to develope an application in Window environment, I would like to reference the dflt file. But it is in binary format and I cannot access it. Anyone can suggest me how to convert the file into text or... (5 Replies)
Discussion started by: user12345
5 Replies

9. UNIX for Advanced & Expert Users

Modifying binary file by editing Hex values ?

Hi , i'm using special binary file (lotus notes) and modifying an hexadecimal address range with windows hex editor and it works fine ! The file is an AIX one and i'm forced to transfert (ftp) it before modifying it and re-transfert ! NOW i would do this directly under AIX ! I can... (4 Replies)
Discussion started by: Nicol
4 Replies

10. Shell Programming and Scripting

Binary and hex in unix

not much familiar with binary and hex calculation in script programming.... explaination: binary format control the parameter turned on or off in the program stored in hex mode, the question is: how to change 39e to 19e using the binary calculation(although i don't know the command for... (2 Replies)
Discussion started by: trynew
2 Replies
Login or Register to Ask a Question