简体   繁体   English

Base64 到 1 和 0 的字符串转换器

[英]Base64 to string of 1s and 0s convertor

I did some search for this question, but I can not find relevant answer.我对这个问题进行了一些搜索,但找不到相关答案。 I am trying to convert some string form input() function to Base64 and from Base64 to raw string of 1s and 0s.我正在尝试将一些字符串形式的 input() function 转换为 Base64 并从 Base64 转换为 1 和 0 的原始字符串。 My converter is working, but its output are not the raw bits, but something like this: b'YWhvag==' .我的转换器正在工作,但它的 output 不是原始位,而是这样的: b'YWhvag==' Unfortunately, I need string of 1s and 0s, because I want to send this data via "flickering" of LED.不幸的是,我需要一串 1 和 0,因为我想通过 LED 的“闪烁”发送这些数据。

Can you help me figure it out please?你能帮我弄清楚吗? Thank you for any kind of help!感谢您提供任何帮助!

import base64

some_text = input()

base64_string = (base64.b64encode(some_text.encode("ascii")))

print(base64_string)

If I have understood it corretly you want binary equivalent of string like 'hello' to ['1101000', '1100101', '1101100', '1101100', '1101111'] each for hello如果我已经正确理解了,您希望字符串的二进制等效项,例如'hello'['1101000', '1100101', '1101100', '1101100', '1101111']每个hello

import base64

some_text = input('enter a string to be encoded(8 bit encoding): ')


def encode(text):
    base64_string = (base64.b64encode(text.encode("ascii")))
    return ''.join([bin(i)[2:].zfill(8) for i in base64_string])

def decode(binary_digit):
    b = str(binary_digit)
    c = ['0b'+b[i:i+8] for i in range(0,len(b), 8)]
    c = ''.join([chr(eval(i)) for i in c])
    return base64.b64decode(c).decode('ascii')

encoded_value = encode(some_text)
decoded_value = decode(encoded_value)

print(f'encoded value of {some_text} in 8 bit encoding is: {encoded_value}')
print(f'decoded value of {encoded_value} is: {decoded_value}')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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