簡體   English   中英

Python:二進制的特殊字符

[英]Python : special char to binary

我正在使用我在這里看到的答案。

我的功能是

def to_bin(s):
    return ' '.join(format(x, 'b') for x in bytearray(s))

它工作得非常好,直到我嘗試轉換'%'字符,我得到輸出:

>>>to_bin('%')
'100101'

而我的預期結果是:

>>>to_bin('%')
'0100101'

你們中有人有解決方案嗎?

提前致謝。

只需將格式說明符更改為使用零填充為七個字符:

def to_bin(s): 
    return ' '.join(format(x, '07b') for x in bytearray(s))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM