Sponsored Content
Top Forums UNIX for Advanced & Expert Users Another binary manipulation thread. Post 302855169 by wisecracker on Thursday 19th of September 2013 09:33:26 AM
Old 09-19-2013
Hi DGP...

Here is an idea using extended characters for 0x00, 0x0A and 0x1B that can easily be imported by the "read" command...
Code:
#!/bin/bash --posix
# Using OSX 10.7.5, bash default terminal...
# Alt-8 = • - zero...
bin_zero="•"
# Alt-7 = ¶ - newline...
new_line="¶"
# Alt-6 = § - escape...
esc_char="§"
# A simple string using •, ¶, and § as a real binary substitutes.
#
echo $bin_zero"This is a binary "$bin_zero$new_line$esc_char" zero, newline and escape character cop-out."$esc_char$bin_zero$new_line > /tmp/bin.dat
# Check it and echo has included a newline character for added fun...
hexdump -C /tmp/bin.dat
#
read bin_text < /tmp/bin.dat
echo "$bin_text"
# Newline is stripped as expected...
#
# Now sawp extended characters into escape characters.
text=$(echo "$bin_text" | sed 's/•/\\0/g;s/¶/\\n/g;s/§/\\\\/g')
# Done!
# Put escape charatcer version of string into a file.
echo -n "$text" > /tmp/newbin.dat
# Done!
# Check that escape charaters for _newline_ and zero exist.
hexdump -C /tmp/newbin.dat
echo "$text"
# Done
# Now resave to the real file with binary 0x00s and 0x0A.
echo -e -n "$text" > /tmp/newbin.dat
# Done
# Prove it exists.
hexdump -C /tmp/newbin.dat
echo "Now converted to binary using extended characters..."
# Yup, it does...

The results:-
Code:
Now converted to binary using extended characters...
AMIGA:barrywalker~> ./bin_test.sh
00000000  e2 80 a2 54 68 69 73 20  69 73 20 61 20 62 69 6e  |...This is a bin|
00000010  61 72 79 20 e2 80 a2 c2  b6 c2 a7 20 7a 65 72 6f  |ary ....... zero|
00000020  2c 20 6e 65 77 6c 69 6e  65 20 61 6e 64 20 65 73  |, newline and es|
00000030  63 61 70 65 20 63 68 61  72 61 63 74 65 72 20 63  |cape character c|
00000040  6f 70 2d 6f 75 74 2e c2  a7 e2 80 a2 c2 b6 0a     |op-out.........|
0000004f
•This is a binary •¶§ zero, newline and escape character cop-out.§•¶
00000000  5c 30 54 68 69 73 20 69  73 20 61 20 62 69 6e 61  |\0This is a bina|
00000010  72 79 20 5c 30 5c 6e 5c  5c 20 7a 65 72 6f 2c 20  |ry \0\n\\ zero, |
00000020  6e 65 77 6c 69 6e 65 20  61 6e 64 20 65 73 63 61  |newline and esca|
00000030  70 65 20 63 68 61 72 61  63 74 65 72 20 63 6f 70  |pe character cop|
00000040  2d 6f 75 74 2e 5c 5c 5c  30 5c 6e                 |-out.\\\0\n|
0000004b
\0This is a binary \0\n\\ zero, newline and escape character cop-out.\\\0\n
00000000  00 54 68 69 73 20 69 73  20 61 20 62 69 6e 61 72  |.This is a binar|
00000010  79 20 00 0a 5c 20 7a 65  72 6f 2c 20 6e 65 77 6c  |y ..\ zero, newl|
00000020  69 6e 65 20 61 6e 64 20  65 73 63 61 70 65 20 63  |ine and escape c|
00000030  68 61 72 61 63 74 65 72  20 63 6f 70 2d 6f 75 74  |haracter cop-out|
00000040  2e 5c 00 0a                                       |.\..|
00000044
Now converted to binary using extended characters...
AMIGA:barrywalker~>

EDIT:

I didn't want to use "sed" but longhand became a large program and defeated the object...

Noticed a minor bug and squashed it...

Last edited by wisecracker; 09-19-2013 at 06:26 PM.. Reason: See above...
 

4 More Discussions You Might Find Interesting

1. Programming

How to cancel a thread safely from the initial thread?

how about asynchronous canceling? or with signal? if with signal whether it effects the process? my english so badly :( :( (1 Reply)
Discussion started by: alan.zhao
1 Replies

2. 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

3. Shell Programming and Scripting

Another Building Block, Binary File Manipulation...

Apologies for any typos, and IF this has been done before... This is yet another building block. The code generates a 256 byte binary file of _characters_ 0x00 to 0xFF for general usage and generates another binary file manipulated in a basic way. I need this facility for a kids project I am... (0 Replies)
Discussion started by: wisecracker
0 Replies

4. Forum Support Area for Unregistered Users & Account Problems

Not able to post thread/reply to thread

Dear Moderator I am not able to post any new thread or post reply to mine old thread. Kindly help as i am stuck on one problem and needed suggestion. Regards Jaydeep (1 Reply)
Discussion started by: jaydeep_sadaria
1 Replies
escape(1)							Mail Avenger 0.8.3							 escape(1)

NAME
escape - escape shell special characters in a string SYNOPSIS
escape string DESCRIPTION
escape prepends a "" character to all shell special characters in string, making it safe to compose a shell command with the result. EXAMPLES
The following is a contrived example showing how one can unintentionally end up executing the contents of a string: $ var='; echo gotcha!' $ eval echo hi $var hi gotcha! $ Using escape, one can avoid executing the contents of $var: $ eval echo hi `escape "$var"` hi ; echo gotcha! $ A less contrived example is passing arguments to Mail Avenger bodytest commands containing possibly unsafe environment variables. For example, you might write a hypothetical reject_bcc script to reject mail not explicitly addressed to the recipient: #!/bin/sh formail -x to -x cc -x resent-to -x resent-cc | fgrep "$1" > /dev/null && exit 0 echo "<$1>.. address does not accept blind carbon copies" exit 100 To invoke this script, passing it the recipient address as an argument, you would need to put the following in your Mail Avenger rcpt script: bodytest reject_bcc `escape "$RECIPIENT"` SEE ALSO
avenger(1), The Mail Avenger home page: <http://www.mailavenger.org/>. BUGS
escape is designed for the Bourne shell, which is what Mail Avenger scripts use. escape might or might not work with other shells. AUTHOR
David Mazieres Mail Avenger 0.8.3 2012-04-05 escape(1)
All times are GMT -4. The time now is 12:25 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy