簡體   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