Search code examples
pythonmicropythonraspberry-pi-pico

Is there a way to convert a string into bits in MicroPython?


While using MicroPython, I recently copied my "toBits()" function from python. My code is this:

def tobits(s):
    bits = ""
    for c in s:
        bits2 = ''.join(format(ord(i), '08b') for i in c)
        bits = bits + bits2
   return bits

However, When using this function, I got the error: "NameError: name 'format' isn't defined"

I'm assuming MicroPython doesn't have "format" in it. Is there a different way to convert a string to bits in MicroPython?


Solution

  • Just figured this out, I ended up replacing the bits2 line with:

    bits2 = ''.join('{:08b}'.format(ord(i)) for i in c)