Modify bit in binary file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Modify bit in binary file
# 1  
Old 04-17-2010
Modify bit in binary file

Hi,

I'm looking for a simple solution to my problem. I want do modify a single bit into a large binary file. The offset of this bit is known and constant.

For example:
Code:
a.bin 		----> 	Operation 	----> 	b.bin
0x100: XXXXXX0 	----> 	Operation 	---->	0x100: XXXXXX1

Because I'm writing a tutorial for beginners this thing should be very easy and no download (scripts, etc) should be done. Is there any possibility to do this?

Thank you all!

Greets,
jodel
# 2  
Old 04-17-2010
Please elaborate on the nature of the modification. Will the case always be as in your example, lowest order bit, which is guaranteed to be zero, toggled to 1 (essentially, just adding 1 to the byte's value)? Or, is the solution required to work on other bits in a byte? And, if the byte is already 1, should it be left unchanged, or should it be toggled to zero? I assume, regardless, that unlike addition, there should be no carry over into higher order bits?
# 3  
Old 04-17-2010
First of all, the goal is to create a second file that differs only in this byte from the original one. The process is only done once.

It would be nice, if the original mask of the byte would not be changed, but at this time this is not a must have. "X" should be don't cares, but assume that they are zeros.

I've found a solution to overwrite a certain byte in a file via dd:
Code:
cp a.bin b.bin
dd of=b.bin bs=1 skip=0x100 seek=0x100 count=1 conv=notrunc

Is there any possibilty to echo non-printable characters via there ascii-code (I need 0x01)? Sorry, I'm new to shell using..

It seems that the BSD-Version of echo doesn't have the '-e' switch..

Last edited by jodel; 04-17-2010 at 05:16 PM..
# 4  
Old 04-17-2010
Code:
printf '\x01'

If you intend to feed the output of printf to dd, you'll probably want to drop the 'skip' option.

Regards,
Alister

Last edited by alister; 04-17-2010 at 05:35 PM..
# 5  
Old 04-17-2010
You're right. It finally works, yeah!

Final version:
Code:
cp a.bin b.bin
printf "\x01" | dd of=b.bin bs=1 seek=0x100 count=1 conv=notrunc
cmp -b -c a.bin b.bin

Thank you, alister! Please close!
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Windows & DOS: Issues & Discussions

Which version of Windows Vista to install with a product key? 32-bit or 64-bit?

Hello everyone. I bought a dell laptop (XPS M1330) online which came without a hard drive. There is a Windows Vista Ultimate OEMAct sticker with product key at the bottom case. I checked dell website (here) for this model and it says this model supports both 32 and 64-bit version of Windows... (4 Replies)
Discussion started by: milhan
4 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

How to handle 64 bit arithmetic operation at 32 bit compiled perl interpreter?H

Hi, Here is the issue. From the program snippet I have Base: 0x1800000000, Size: 0x3FFE7FFFFFFFF which are of 40 and 56 bits. SO I used use bignum to do the math but summing them up I always failed having correct result. perl interpreter info, perl, v5.8.8 built for... (0 Replies)
Discussion started by: rrd1986
0 Replies

4. Linux

Request: Compile Earlier Version of XTIDE and upload XTTPD binary on 64 bit Linux

Can someone try to compile this older version of xtide (attached) on 64-bit Intel/Linux and upload the xttpd binary? I have a friend who needs an older version up and running because the newer version does not support his required harmonic files. Thanks! (7 Replies)
Discussion started by: Neo
7 Replies

5. Shell Programming and Scripting

search 32 bit binary for the 1's and return a 1 to the placeholder variable

Folks , I have a korn shell script that i have written for assembly, the variable that is a final result is returning hexadecimal, now the value is to be converted to binary and return the place holder in the binary that has a 1 in its place and send it to a variable assigned for the... (0 Replies)
Discussion started by: venu
0 Replies

6. Solaris

compiled binary file gives "cannot execute binary file"

Hi, I have two Solaris machines. 1. SunOS X 5.8 Generic_108528-29 sun4u sparc SUNW,Sun-Blade-1500 2. SunOS Y 5.8 Generic_108528-13 sun4u sparc SUNW,Ultra-60 I am trying to buiild a project on both these machines. The Binary output file compiled on machine 2 runs on both the machines. Where... (0 Replies)
Discussion started by: scgupta
0 Replies

7. Programming

copying or concatinating string from 1st bit, leaving 0th bit

Hello, If i have 2 strings str1 and str2, i would like to copy/concatenate str2 to str1, from 1st bit leaving the 0th bit. How do i do it? (2 Replies)
Discussion started by: jazz
2 Replies

8. Programming

Writing both 8-bit and 16-bit data to a file

I'm writing both 8-bit and wide 16-bit data to the screen and an output file. I have no problems with writing out to the screen - for example: cout<<8-bit data; wcout<<16-bit data; Similarly, I have used ofstream for 8-bit and wofstream for 16-bit, for example: ofstream out; wofstream wout;... (1 Reply)
Discussion started by: Breen
1 Replies
Login or Register to Ask a Question