繁体   English   中英

有没有办法在 MicroPython 中将字符串转换为位?

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

在使用 MicroPython 时,我最近从 python 复制了我的“toBits()”函数。 我的代码是这样的:

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

但是,使用此功能时,我收到错误:“NameError: name 'format' is not defined”

我假设 MicroPython 中没有“格式”。 在 MicroPython 中是否有其他方法可以将字符串转换为位?

刚刚想通了这一点,我最终将 bits2 行替换为:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM