Hướng dẫn string to binary python

If by binary you mean bytes type, you can just use encode method of the string object that encodes your string as a bytes object using the passed encoding type. You just need to make sure you pass a proper encoding to encode function.

In [9]: "hello world".encode['ascii']                                                                                                                                                                       
Out[9]: b'hello world'

In [10]: byte_obj = "hello world".encode['ascii']                                                                                                                                                           

In [11]: byte_obj                                                                                                                                                                                           
Out[11]: b'hello world'

In [12]: byte_obj[0]                                                                                                                                                                                        
Out[12]: 104

Otherwise, if you want them in form of zeros and ones --binary representation-- as a more pythonic way you can first convert your string to byte array then use bin function within map :

>>> st = "hello world"
>>> map[bin,bytearray[st]]
['0b1101000', '0b1100101', '0b1101100', '0b1101100', '0b1101111', '0b100000', '0b1110111', '0b1101111', '0b1110010', '0b1101100', '0b1100100']
 

Or you can join it:

>>> ' '.join[map[bin,bytearray[st]]]
'0b1101000 0b1100101 0b1101100 0b1101100 0b1101111 0b100000 0b1110111 0b1101111 0b1110010 0b1101100 0b1100100'

Note that in python3 you need to specify an encoding for bytearray function :

>>> ' '.join[map[bin,bytearray[st,'utf8']]]
'0b1101000 0b1100101 0b1101100 0b1101100 0b1101111 0b100000 0b1110111 0b1101111 0b1110010 0b1101100 0b1100100'

You can also use binascii module in python 2:

>>> import binascii
>>> bin[int[binascii.hexlify[st],16]]
'0b110100001100101011011000110110001101111001000000111011101101111011100100110110001100100'

hexlify return the hexadecimal representation of the binary data then you can convert to int by specifying 16 as its base then convert it to binary with bin.

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Data conversion have always been widely used utility and one among them can be conversion of a string to it’s binary equivalent. Let’s discuss certain ways in which this can be done.

    Method #1 : Using join[] + ord[] + format[]
    The combination of above functions can be used to perform this particular task. The ord function converts the character to it’s ASCII equivalent, format converts this to binary number and join is used to join each converted character to form a string.

    test_str = "GeeksforGeeks"

    print["The original string is : " + str[test_str]]

    res = ''.join[format[ord[i], '08b'] for i in test_str]

    print["The string after binary conversion : " + str[res]]

    Output :

    The original string is : GeeksforGeeks
    The string after binary conversion : 01000111011001010110010101101011011100110110011001101111011100100100011101100101011001010110101101110011

    Method #2 : Using join[] + bytearray[] + format[]
    This method is almost similar to the above function. The difference here is that rather than converting the character to it’s ASCII using ord function, the conversion at once of string is done by bytearray function.

    test_str = "GeeksforGeeks"

    print["The original string is : " + str[test_str]]

    res = ''.join[format[i, '08b'] for i in bytearray[test_str, encoding ='utf-8']]

    print["The string after binary conversion : " + str[res]]

    Output :

    The original string is : GeeksforGeeks
    The string after binary conversion : 01000111011001010110010101101011011100110110011001101111011100100100011101100101011001010110101101110011
    


    Chủ Đề