Hi, I got stuck in third steps of this problem for a while, can you explain how
ID: 3885521 • Letter: H
Question
Hi, I got stuck in third steps of this problem for a while, can you explain how to manipulate data of binary file after stored it in the dynamic memory address, it would be really nice if you can go through everything in this code.
2) Required function: void unzip file(const std:string& filename) You get to work on your program to obfuscate a file and write a function named "unzip file." Your idea is to treat a file as blocks of 4 bytes of data and reorder these blocks so that to the world, this looks like nothing more than random data. To do this, you should open the file in binary mode and move every even positioned "block" to the end of the file, maintaining their order and leaving no space between each block. At the end, all odd positioned blocks are in increasing order, and all even positioned blocks should be in increasing order, where all even positions follow all odd positions. This output should be stored in a new file with the same name as the original file, but with the added suffix"-unzipped". EXAMPLE: Suppose you have a 32-byte file named "test" as follows (in hex): 0102030405060708091011121314151617181920212223242526272829303132 If we treat everything in the file groups of 4 bytes, we can view the file as the following 8 blocks: 1 2 3 4 1I 5 6 I7 8 0102030405060708091011121314151617181920212223242526272829303132 After running your function unzip file on this file "test", the blocks should be reordered as follows and the resulting data should be written to a file named "test-unzipped": 1 3 5 10 7 2 1 4 6 8 0102030409101112171819202526272805060708131415162122232429303132 To accomplish this: Open the file with the given filename for input in binary mode. · Read the entire file contents (you may want to compute the file size as well). Manipulate the data. write the output data to a file with the same name as the input file, but with the added suffix"-unzipped" · Note: you should not modify the original file when doing this.Explanation / Answer
Given problem leverage more of an algorithm & less of programming.
The heart of the problem is 'Manipulation' of the binary file as per the odd/even combinations. Clearly, the algorithmic steps are as follows :
1. Open the file in binary mode. That is "Read Binary mode"
2. Once you open the file, you need to divide it into chunks of 4 bytes. (Think of it as 'n' chunks to make it a general solution)
3. Now, you can use a list slice mechanism to reorder the content. In the world of programming, its called "Slicing". for example,
If a list called Alpha = ['a', 'b', 'c', 'd', 'e', 'f']
Then Slice(Alpha, 1) would give ['a', 'c', 'e'] .
This is what we excatly need in this problem.
4. Produce 'odd' slice. Then produce 'even' slice. Lastly, 'Join' these two slices.
5. Next, write content to the output file with renaming. This would be simple.
Enough of theory right! Let's look at the programmatic way to solve this. As the language is not specified, I will use Python 2.7 as solution.
Following is the Python code for given problem -
#Open file in binary mode
f = open(FileName, 'rb')
#Define read function to get data in the block of 4 bytes
def Read4Bytes():
return f.read(4)
data = [] #Initialize a list
#Ierate over file contents to fetch data in 4 Bytes chunk
for piece in iter(Read4Bytes, ''):
data.append(piece)
l_odd = data[1::2] ## Slice odd positioned data
l_even = data[0::2] ## Slice even positioned data
l_ReorderData = l_odd + l_even ## Reorder data
print l_ReorderData
# Write data to new file.
f = open(FileName, 'wb')
for data in l_ReorderData:
f.write(data)
f.close()
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.