Question

In python, how can I pad an array of bytes so that all values are 8...

In python, how can I pad an array of bytes so that all values are 8 bytes long?

#bytes to parse

byte = b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'

#parse bytes to all be 8 long in the array

blocklist = [inpbyteseq[i:i+8] for i in range(0,len(inpbyteseq), 8)]

#Pad the bytes at the end of the array so that they are 8 long as well

    for i in blocklist:
        while (sys.getsizeof(i) < 41):
            blocklist[i] = blocklist[i] + b'\x20' * (41 - sys.getsizeof(i))

This returns an error. What am I doing wrong?

0 0
Add a comment Improve this question Transcribed image text
Answer #1

THE CODE IS:

# bytes to parse

byte = b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'

# taking user input as string and then converting it into bytes using str.encode()
inpbyteseq = str.encode(input('Enter a string\n'))

# parse bytes to all be 8 long in the array
# creating a list of sequences of length 8 bytes
blocklist = [inpbyteseq[i:i + 8] for i in range(0, len(inpbyteseq), 8)]

# Pad the bytes at the end of the array so that they are 8 long as well
# iterating through each sequence in blocklist
# i is index of the sequence in the blocklist
for i in range(len(blocklist)):
    if len(blocklist[i]) < 8:
        blocklist[i] = blocklist[i] + b'\x20' * (8 - len(blocklist[i]))

print(blocklist)

SCREENSHOT TO UNDERSTAND INDENTATION:

OUTPUT:

Add a comment
Know the answer?
Add Answer to:
In python, how can I pad an array of bytes so that all values are 8...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need a Python code for this problem. We can use python's array slicing in many...

    I need a Python code for this problem. We can use python's array slicing in many ways, and here is just one example. To take the forward derivative of an array y, we use (y[i+1] - y[i])/dx For example, if dx=1 , we might write a derivative routine as yderiv = zeros (len(y)-1) for i in range(len(y)-1): yderiv[i] = y(i+1] - y[i] Note that here, yderiv is one element shorter than y -- this is because you need 2 points...

  • I am using python and I want to know how I can get the spacing between...

    I am using python and I want to know how I can get the spacing between the low mid and high to be dependent on the range and how I can get that boarder to be dependent on the range as well. Im not sure what you mean, I just want to know about the spacing. Modify your project1.py Python program so that it displays the low, mid and high point values for periodic function and a double-line border. (HINT:...

  • Python Problem Hello i am trying to finish this code it does not save guesses or...

    Python Problem Hello i am trying to finish this code it does not save guesses or count wrong guesses. When printing hman it needs to only show _ and letters together. For example if the word is amazing and guess=a it must print a_a_ _ _ _ not a_ _ _ _ and _a_ _ _ separately or with spaces. The guess has a maximum of 8 wrong guesses so for every wrong guess the game must count it if...

  • In the project I am working right now, we have some python and some C# code....

    In the project I am working right now, we have some python and some C# code. At some point, I call from python a subprocess which starts a C# executable. This C# code returns an error code, which has to be understood on the python side and a readable report has to be created. This means, both sides "must speak the same language". So, at the end I have to handle a mapping {error codes: readable explanation} (normally we implement...

  • I have to follow functions to perform: Function 1: def calculate_similarity(data_segment, pattern): The aim of this func...

    I have to follow functions to perform: Function 1: def calculate_similarity(data_segment, pattern): The aim of this function is to calculate the similarity measure between one data segment and the pattern. The first parameter 'data_segment' is a list of (float) values, and the second parameter 'pattern' is also a list of (float) values. The function calculates the similarity measure between the given data segment and pattern and returns the calculated similarity value (float), as described earlier in this assignment outline. The...

  • Python Programming: Why do I get UnboundLocalError? What does this mean and how can I fix...

    Python Programming: Why do I get UnboundLocalError? What does this mean and how can I fix it? Question: Write a function check_move(column, row) which returns a string describing a chess move to a given row and column on the chess board. Your program must check if the row and column entered are both valid. The column in a chess board is a letter ranging from A to H or a to h (inclusive) and the row is a number between...

  • Python Project

    AssignmentBitmap files map three 8-bit (1-byte) color channels per pixel. A pixel is a light-emitting "dot" on your screen. Whenever you buy a new monitor, you will see the pixel configuration by its width and height, such as 1920 x 1080 (1080p) or 3840x2160 (4K). This tells us that we have 1080 rows (height), and each row has 1920 (width) pixels.The bitmap file format is fairly straight forward where we tell the file what our width and height are. When...

  • How can I get my Python code to check to make sure there are no ships...

    How can I get my Python code to check to make sure there are no ships overlapping in my Battleship code? I am having a really hard time with a game of battleship that I have due for class. Each time I run my code, It will print the ships on the board but it will at times overlap them. I know I have to use some kind of nested if statements but I just don't get how or where...

  • In python, how do you create an array of unknown dimensional size? I want to create...

    In python, how do you create an array of unknown dimensional size? I want to create an array of unknown dimensional size based on a number I recieve. To elaborate, let's say n = 2, it will output a 3D array {true, true, true}. If n = 3, then its output will be a 4D array {true, true, true, true}. Whatever the n value is, the return value will be an array of n + 1 dimensional size. How would...

  • Consider a non-empty int array ints. A contiguous subarray ints[ start .. start + len -1...

    Consider a non-empty int array ints. A contiguous subarray ints[ start .. start + len -1 ] (with starting index start and length len) is called a flat if all elements of that subarray are equal. Furthermore, such a subarray is called a plateau if it is flat and each of the elements ints[start -1] and ints[start + len] that immediately proceed/succeed the subarray are either nonexistent (i.e., out of array’s index range) or are strictly smaller than the elements...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT