Sponsored Content
Top Forums UNIX for Advanced & Expert Users Another binary manipulation thread. Post 302851089 by wisecracker on Saturday 7th of September 2013 06:21:03 AM
Old 09-07-2013
You have whetted my apetite again... ;o)

There is possibly one _bug_ I can see in both my and your attempts is character 92, "\"
causing a pseudo escape on the next binary character it sees when running an
"somevar=echo -e......" into it more than once.

It also seems your 0x09 and 0x0A become NULL too.

I can understand why 0x0A _might_ occur but not 0x09, (Tab).
Quote:
Does read differentiate that from EOF in a while read loop?
On a binary file with single byte zero(s) uisng the default bash "read", yes, but on a
binary_string_variable, hmmm, hard to judge as placing byte zero into a variable is
not possible at this point.

Ignoring "read" FTTB I am going to try and force byte zero into the middlie of
a "Hello World." string, i.e. the space replaced with 0x00...

---------- Post updated 07-09-13 at 11:21 AM ---------- Previous update was 06-09-13 at 10:58 PM ----------

Hi DGPickett...
I tried this out from the command line and......
Code:
Last login: Sat Sep  7 10:17:43 on ttys000
AMIGA:barrywalker~> text="Hello World."
AMIGA:barrywalker~> text=${text/ /"\0"}
AMIGA:barrywalker~> echo ${#text}
13
AMIGA:barrywalker~> echo -n $text > /tmp/txt
AMIGA:barrywalker~> hexdump -C /tmp/txt
00000000  48 65 6c 6c 6f 5c 30 57  6f 72 6c 64 2e           |Hello\0World.|
0000000d
AMIGA:barrywalker~> echo -e -n $text > /tmp/txt
AMIGA:barrywalker~> hexdump -C /tmp/txt
00000000  48 65 6c 6c 6f 00 57 6f  72 6c 64 2e              |Hello.World.|
0000000c
AMIGA:barrywalker~> echo -e "$text"
HelloWorld.
AMIGA:barrywalker~> echo "$text"
Hello\0World.
AMIGA:barrywalker~> read newtext < /tmp/txt
AMIGA:barrywalker~> echo $newtext
Hello
AMIGA:barrywalker~> echo ${#newtext}
5
AMIGA:barrywalker~> # 0x00 always strips anything after it...
AMIGA:barrywalker~>
AMIGA:barrywalker~> echo -n $text > /tmp/txt
AMIGA:barrywalker~> read newtext < /tmp/txt
AMIGA:barrywalker~> echo $newtext
Hello0World.
AMIGA:barrywalker~> echo ${#newtext}
12
AMIGA:barrywalker~> hexdump -C /tmp/txt
00000000  48 65 6c 6c 6f 5c 30 57  6f 72 6c 64 2e           |Hello\0World.|
0000000d
AMIGA:barrywalker~> _

"read" has brought in the correct length of "Hello World.", 12, with the space shown as
character 0, but its file length is actually 13 characters in size...

So to return back to 13 characters again using pseudo-zero 2 backslashes will be needed
inside the file now making the file length 14.

This is a real killer for binary_string to file and back again manipulation.
I think the only way to account for binary zero is to use "\0" two characters and detect
those two characters inside the binary_string something like this...
Code:
if [ "${binary_string:$some_position:2}" == "\0" ]
then
        echo "\0 at position $some_position in binary string."
        # Do other binary string manipulation as required.
fi

The problem is that there may be two genuine independent "\" and "0" characters side
by side and not related to binary zero at all, just independent characters...

This is getting more difficult by the day... ;o)
 

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
All times are GMT -4. The time now is 02:42 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy