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.
Python3
# Python3 code to demonstrate working of # Converting String to binary # Using join() + ord() + format() # initializing string test_str = "Lazyroar" # printing original string print ("The original string is : " + str (test_str)) # using join() + ord() + format() # Converting String to binary res = ' '.join(format(ord(i), ' 08b ') for i in test_str) # printing result print ("The string after binary conversion : " + str (res)) |
The original string is : Lazyroar The string after binary conversion : 01000111011001010110010101101011011100110110011001101111011100100100011101100101011001010110101101110011
Time Complexity: O(N) where N is the lenght of the input string.
Auxiliary Space: O(N)
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.
Python3
# Python3 code to demonstrate working of # Converting String to binary # Using join() + bytearray() + format() # initializing string test_str = "Lazyroar" # printing original string print ("The original string is : " + str (test_str)) # using join() + bytearray() + format() # Converting String to binary res = ' '.join(format(i, ' 08b ') for i in bytearray(test_str, encoding =' utf - 8 ')) # printing result print ("The string after binary conversion : " + str (res)) |
The original string is : Lazyroar The string after binary conversion : 01000111011001010110010101101011011100110110011001101111011100100100011101100101011001010110101101110011
Method #3 : Using join() + bin() + zfill()
we define a function str_to_binary(string) that takes in a string as its input.
We then create an empty list called binary_list, which will be used to store the binary conversions of each character in the input string.
We then use a for loop to iterate through each character in the input string. For each character, we use the ord() function to convert it to its ASCII equivalent, and then use the bin() function to convert that integer to a binary string. The bin() function returns a string with a ‘0b’ prefix, so we use list slicing to remove that prefix.
After that we use the zfill(8) method to pad the binary conversion with leading zeroes until it reaches a length of 8. The zfill() method pads the string on the left with a specified character (in this case, ‘0’) until it reaches the desired length.
Then we append the binary conversion to the binary_list and after that we join all the binary values in the list and return as a single string by using join() method.
Finally, we test the function with an example input of the string “Lazyroar”. The function returns the binary string representation of the input string.
Python3
def str_to_binary(string): # Initialize empty list to store binary values binary_list = [] # Iterate through each character in the string for char in string: # Convert character to binary, pad with leading zeroes and append to list binary_list.append( bin ( ord (char))[ 2 :].zfill( 8 )) # Join the binary values in the list and return as a single string return ''.join(binary_list) # Test with example input test_str = "Lazyroar" print (str_to_binary(test_str)) #This code is contributed by Edula Vinay Kumar Reddy |
01000111011001010110010101101011011100110110011001101111011100100100011101100101011001010110101101110011
Time complexity: O(n), where n is the length of the input string.
Auxiliary Space: O(n), as we store each binary conversion in a list.
Method 4 : Using the binascii module.
Step-by-step approach:
- Import the binascii module.
- Initialize the string you want to convert to binary.
- Use the binascii.hexlify() function to convert the string to hexadecimal format.
- Use the binascii.unhexlify() function to convert the hexadecimal string to binary format.
- Decode the binary format using the .decode() method and store the result in a variable.
- Print the original string and the binary format.
Python3
# importing the binascii module import binascii # initializing the string to convert test_str = "Lazyroar" # converting the string to binary using binascii hex_str = binascii.hexlify(test_str.encode()) bin_str = bin ( int (hex_str, 16 ))[ 2 :].zfill( 8 * (( len (hex_str) + 1 ) / / 2 )) res = bin_str # printing the original string and the binary format print ( "The original string is: " + str (test_str)) print ( "The string after binary conversion: " + str (res)) |
The original string is: Lazyroar The string after binary conversion: 01000111011001010110010101101011011100110110011001101111011100100100011101100101011001010110101101110011
Time complexity: O(n)
Auxiliary space: O(n).