0

I'm writing the following string in my output file:

bitstream.add("stri", 32)

where

def add(self, data, length):
    s = ''
    if (type(data) == str):
        for char in data:
            b = bin(ord(char))[2:]
            s = s + "{:0>8}".format(b)
    else:
        s = bin(data)[2:]
        if (len(s) < length):
            resto = length - len(s)
            for _ in range(0, resto):
                s = '0' + s
    s = s[0:length]
    self.cache = self.cache + s
    self.flush()

Later on I need to read the string from the output file. I use Python struct unpack module as follows:

   from struct import unpack
   key_length_bytes = 32
   key = ""
   for _ in range(0, key_length_bytes):
      carattere = chr(unpack('>B', in_file.read(1))[0])
      key = "%s%s" (key, carattere)

I get

key = "%s%s" (key, carattere)
TypeError: 'str' object is not callable

Thank you for any help you could provide.

1

1 Answer 1

2

You're missing a % sign. key = "%s%s" (key, carattere) needs to be changed into

key = "%s%s" % (key, carattere)
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, problem solved! I also edited the question since the loop to read the string was not correct.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.