Bytes and Bytearrays

Indices and tables

Background

The bytes and bytearray array types are Python’s fully unicode compliant way to handle raw binary data values. Being able to manipulate them confidently is a necessary skill when moving data between computers using disk files or network protocols. These exercises will give you some exposure to working with these data types.

Exercise

  1. Create an array of 15 bytes initialized to 0.
  2. Create an array of 15 bytes, with a start value of 1 and incrementing by 2.
  3. Create an array of 10 bytes all with the value 0xFF. Then, change every 5th value to be the byte value 0x00.
  4. Create 2 arrays of 4 bytes all with the value 170. Join the 2 arrays with the value 0x00.
  5. Create a bytes object that holds the control codes for a new-line followed by a null character.
  6. Convert the copyright symbol followed by the integer “2018” into a bytes representation using the UTF-8 encoding for strings.
  7. Convert the telephone receiver symbol, and a 10 digit phone number, into a bytes representation using the UTF-16 encoding for strings.

For the questions below, use the following binary sequence:

b'\xe2\x98\xa2\x20\x43\x61\x6c\x6c\x20\x46\x61\x63\x69\x6c\x69\x74\x69\x65\x73'
  1. Convert the binary sequence back into a string using UTF-8 decoding and display it.
  2. Programmatically determine the number of times the value b’x6c’ occurs in the binary sequence.
  3. Confirm whether the value b’x20’ occurs in the binary sequence.
  4. Confirm whether the binary sequence starts with b’xe2’.
  5. Split the binary string on each occurrence of b’x20’.

Hints

  • It might be useful to review how to create string characters using escape values, in the lesson on Strings.
  • The unicode tables here can help you with string <-> byte value mappings.

Solution

When you are ready to see one possible solution, download and open this Jupyter Notebook file