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
# 1  
Old 09-04-2015
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 the below command(od) but i want all the records/lines to be arranged accordingly so that further processing can be done.


Code:
od -t x1 DDI15.09.02.C

Some lines from the above command is as follows
Code:
0000000 84 7d 00 0c 09 40 11 8a 14 93 51 51 72 74 0f 09
0000020 02 0f 05 26 0d 65 06 51 55 21 67 01 00 00 6e 06
0000040 06 a0 5a 6e 7a 0b 00 82 05 10 00 00 8e 0a 03 13
0000060 8a 14 93 51 51 72 98 09 03 04 46 04 04 32 04 9a
0000100 07 01 05 00 20 00 a8 08 01 10 06 51 55 21 c6 0f
0000120 01 05 b0 00 00 02 08 29 00 00 a9 00 00 cc 18 01
0000140 03 01 03 04 92 18 04 04 92 18 05 08 02 92 18 00
0000160 01 47 06 03 01 96 08 af 00 cf 14 00 00 84 86 00
0000200 0c 08 40 11 6a 14 15 10 77 60 74 0f 09 02 0f 03
0000220 01 01 65 0a 94 14 10 92 15 67 03 00 00 6c 4f 56
0000240 4f 44 41 4c 07 00 09 6e 06 4f b1 85 aa 7a 2a 00
0000260 80 0c 03 12 0e 41 03 94 14 10 92 15 82 05 10 00
0000300 00 8e 0a 03 13 6a 14 15 10 77 60 a8 0a 01 10 0a
0000320 94 14 10 92 15 c6 0f 01 05 d8 06 00 02 08 26 00
0000340 00 80 00 00 cc 17 01 03 01 02 03 30 03 04 4b 18
0000360 05 08 02 4b 18 00 00 26 06 03 01 96 08 b0 00 cf
0000400 14 00 00 84 86 00 0c 08 40 11 6a 29 15 10 40 85
0000420 74 0f 09 02 0f 05 0f 01 65 0a 98 28 54 86 51 67
0000440 01 00 00 6c 4f 56 4f 44 41 4c 08 00 1f 6e 06 9d
0000460 90 0a 18 7a 2a 00 80 0c 03 12 0e 41 03 98 28 54
0000500 86 51 82 05 10 00 00 8e 0a 03 13 6a 29 15 10 40
0000520 85 a8 0a 01 10 0a 98 28 54 86 51 c6 0f 01 05 9b
0000540 01 00 02 08 54 00 00 60 00 00 cc 17 01 03 01 02
0000560 03 30 03 04 f9 17 05 08 02 f9 17 00 03 42 06 03
0000600 01 96 08 b1 00 cf 14 00 00 84 91 00 0c 10 40 11
0000620 6a 14 15 15 32 24 74 0f 09 02 0f 00 27 0b 65 0a
0000640 93 51 68 48 70 66 02 00 00 67 06 00 00 6c 42 52
0000660 4c 4c 4f 43 03 00 0e 6e 06 37 f1 59 fe 7a 2a 00
0000700 80 0c 03 12 0e 31 25 93 51 68 48 70 82 05 10 00
0000720 00 8e 0a 03 13 6a 14 15 15 32 24 a8 0a 01 10 0a
0000740 93 51 68 48 70 ac 0b 12 03 10 6a 14 15 15 32 24
0000760 c6 0f 01 05 63 0c 00 02 08 05 00 00 5f 00 00 cc
0001000 13 01 03 04 02 03 30 05 08 00 04 00 37 01 12 06
0001020 03 01 96 08 b2 00 cf 14 00 00 84 91 00 0c 10 40
0001040 11 6a 14 15 15 32 25 74 0f 09 02 0f 05 0a 05 65

Output required 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

and so on....
# 2  
Old 09-04-2015
Hello siramitsharma,

Could you please attach the actual file, I was having issues with opening it.

Thanks,
R. Singh

Last edited by RavinderSingh13; 09-04-2015 at 07:14 AM..
# 3  
Old 09-04-2015
PFA the zip file again.
# 4  
Old 09-04-2015
Do you have the command hexdump?

Something like:-
Code:
 ARRAY=( `hexdump -v -e '1/1 "%02x "' /path/to/filename` )

For a sapce delimited flat array, or:-
Code:
 VAR=`hexdump -v -e '1/1 "%02x "' /path/to/filename`

As complete string pairs with a space between each pair, flat variable.
Once you have either __editing__ is easy...

EDIT:
If you have only od then try:-
Code:
 ............ od -tx1 -An ............

Note the -An part and again using a similar approach to 'hexdump' above...
(Check to see you have them using 'man od'.)

Last edited by wisecracker; 09-04-2015 at 12:11 PM.. Reason: See above...
# 5  
Old 09-04-2015
Not sure if this will work with your large files:
Code:
od -tx1 -w655360 -An | sed 's/\(00 00\) \(84\)/\1\n\2/g'

od might not want a width that large, and sed might be overburdened with lines that long.
# 6  
Old 09-05-2015
Thanks wisecracker & RudiC..
# 7  
Old 09-07-2015
Thanks Rudi & wisecracker i have used 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

Post this i have a problem writing the processed file against the hex generated above. Below is the code for reference


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

Output coming is as follows:
Code:
1493515172 00/00/00 00:00:00
1415107760 00/00/00 00:00:00
2915104085 00/00/00 00:00:00
1415153224 00/00/00 00:00:00
1415153225 00/00/00 00:00:00
1415109892 00/00/00 00:00:00
1415118252 00/00/00 00:00:00
2945107422 00/00/00 00:00:00
291515250 00/00/00 00:00:00
1415166271 00/00/00 00:00:00

while when running the timestamp function along required output is coming. Can you please help
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