[英]Is there a way to convert a string into bits in MicroPython?
While using MicroPython, I recently copied my "toBits()" function from python.在使用 MicroPython 时,我最近从 python 复制了我的“toBits()”函数。 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"但是,使用此功能时,我收到错误:“NameError: name 'format' is not defined”
I'm assuming MicroPython doesn't have "format" in it.我假设 MicroPython 中没有“格式”。 Is there a different way to convert a string to bits in MicroPython?
在 MicroPython 中是否有其他方法可以将字符串转换为位?
Just figured this out, I ended up replacing the bits2 line with:刚刚想通了这一点,我最终将 bits2 行替换为:
bits2 = ''.join('{:08b}'.format(ord(i)) for i in c) bits2 = ''.join('{:08b}'.format(ord(i)) for i in c)
我知道这适用于常规 Python,不确定 MicroPython。
bits = ''.join(bin(i)[2:].rjust(8,'0') for i in c)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.