简体   繁体   English

字符串的二进制表示

[英]Binary representation of string

Assuming I have some ASCII characters in a string, let's say s = ABC , how can I retrieve the binary representation as a string? 假设我在字符串中有一些ASCII字符,例如s = ABC ,我如何才能将二进制表示形式作为字符串检索呢?

In this case, 在这种情况下,

A = '01000001'
B = '01000010'
C = '01000011'

so I want something like make_binary('ABC') to return '010000010100001001000011' 所以我想要像make_binary('ABC')这样的东西返回'010000010100001001000011'

I know I can get the hex values for a string. 我知道我可以获取字符串的十六进制值。 I know I can get the binary representation of an integer. 我知道我可以得到整数的二进制表示形式。 I don't know if there's any way to tie all these pieces together. 我不知道是否有办法将所有这些部分捆绑在一起。

Use the ord() funcction to get the integer encoding of each character. 使用ord()函数可获取每个字符的整数编码。

def make_binary(s):
    return "".join([format(ord(c), '08b') for c in s])
print(make_binary("ABC"))

08b formatting returns the number formatted as 8 bits with leading zeroes. 08b格式化返回格式化为8位且前导零的数字。

I think the other answer is wrong. 我认为另一个答案是错误的。 Maybe I interpret wrongly the question. 也许我误解了这个问题。

In any case, I think you are asking for the 'bit' representation. 无论如何,我认为您要求的是“位”表示。 Binary often is used for bytes representation (the .bin files, etc.) 二进制通常用于字节表示(.bin文件等)

The byte representation is given by an encoding, so you should encode the string, and you will get a byte array. 字节表示形式是由编码给出的,因此您应该对字符串进行编码,然后将获得一个字节数组。 This is your binary (as byte) representation. 这是您的二进制(以字节为单位)表示形式。

But it seems you are asking 'bit-representation'. 但似乎您在问“位表示”。 That is different (and the other answer, IMHO is wrong). 那是不同的(另一个答案,恕我直言是错误的)。 You may convert the byte array into bit representation, like on the other answer. 您可以像其他答案一样将字节数组转换为位表示形式。 Note: you are converting bytes. 注意:您正在转换字节。 The other answer will fails on any characters above 127, by showing you only the binary representation of one byte. 通过仅显示一个字节的二进制表示形式,其他答案将对127以上的任何字符均失败。

So: 所以:

def make_binary(s):
    return "".join(format(c, '08b') for c in s.encode('utf-8'))

and the test (which file on @Barmar answer). 和测试(@Barmar上的哪个文件)。

>>> print(make_binary("ABC"))
010000010100001001000011
>>> print(make_binary("Á"))
1100001110000001

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

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