Many people they tend to forget the basics of converting things from a specific data types to another data type like converting from integer to Binary or characters to Binary.
Is important to use Convertions especially when dealing with AI and ML.
a = 8 binary_form = format(int(a), 'b') print(str(binary_form), end='')
The built-in function format() is used to format string using a controlled by specifier. If want to learn more about this go to:https://www.programiz.com/python-programming/methods/built-in/format
The syntax is:
Now we have dealt with converting integer to Binary.
But now we are going to deal with converting characters to Binary this will help with Creating chatbot using binary algorithm I am about to drop soon.
Let's say we have a sentence 'hello world' and want to convert it Binary so our bot can find a perfect response that is smart.
Text: hello world
Step 1: convert the phrase to a list of individual characters
hello_world = 'hello world' char_list = [x for x in hello_world] #There are many ways to do this but #you can use any ways. #I believe this is not the best #way to convert to character list print(char_list)
['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd']
Step 2: Convert each character to an integer using built-in function ord()
#Create a temporary list to store #integers temp = list() #now loop in the list of chars for character in char_list: temp.append(ord(character)) print(temp)
[104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100]
Step 3: Convert each element in to binary
#we need a second temp list #lets call it temp2 temp2 = list() for digit in temp: bin_format = format(digit, 'b') temp2.append(bin_format) print(temp2)
['1101000', '1100101', '1101100', '1101100', '1101111', '100000', '1110111', '1101111', '1110010', '1101100', '1100100']
I shared this post to understand the process behind convert character to Binary. I know this may not be the best solution but it got the job done. Now is up to you to find yourself the best solution