Convert Text To Binary in Python

NeuralNine · Beginner ·💻 AI-Assisted Coding ·3y ago

Key Takeaways

This video demonstrates how to convert text to binary and back in Python using the ASCII code, join function, and list comprehension without external modules or imports.

Full Transcript

what is going on guys welcome back in today's video we're going to learn how to take text in Python and turn it into a binary format which means that we take something like a text message and turn it into a series of zeros and ones so let us get right into it not a [Music] game all right so this one's going to be quite simple we're going to take a simple message turn it into a sequence of zeros and ones then we're going to take that result sequence and turn it back into human readable text in Python and all of this will be done with core python without any external modules and even without importing anything from the core python stack so we're going to use just the basic functions available without using any Imports so a quite fundamental task here a quite basic task uh and we're going to start here with a simple message object which is going to say hello world this is my message and this is something that we want to encode now uh into a sequence of zeros and ones and in order to do that what we're going to do is we're going to take the individual characters h e l and so on and we're going to get the aski code from python we're going to take that numerical value and we're going to turn it into a binary value so we're going to take the number uh for example if you have the number three that would be in binary 1 one because 1 * 1 1 * 2 uh and 111 would be seven because you have 1 * 4 1 * 2 1 * 1 which in fact is seven and if you have something like one 1 it would be 4 + 1 which would be five so we're going to just take the asky code and turn it into a binary number and then we're going to take all these uh binary values and we're going to um to use white spaces to separate them so each character will be one sequence of zeros and ones separated uh so the individual characters are going to be separated by wi spaces this is the idea and in order to do that we're going to say now binary equals and in order to get the asky code of one character in Python maybe before we get the actual result let's play around with this a little bit in order to get the asky code of one specific uh character in Python we use the so-called ort function so if I say for example or a capital A this will return to me um the the value 65 so if I say lowercase a that will be a different value 97 as you can see here um and we can get any asky code of any character that is of course U supported by the asky encoding uh we can get it with the or function so all we need to do is we need to take each individual character here including the white spaces and encode it or or calculate or get the asky code using the or function and then what we're going to do is we're going to get this uh number this resulting number and format it into a binary format so we're going to use another function called format we're going to pass to it the or function uh with with the respective character and we're going to say that the format is going to be B which is binary and as a result of that we're going to get this number here so this here is the binary representation of the aski code of a and we're going to do that for each individual character and we're going to combine all of them together uh separated by white spaces and in order to do that we're going to use these three functions in a rle so we're going to say binary and then we're going to use a whites space here as the connector so to say and we're going to say do join basically for those of you who don't know what the join function does is it takes this separator here and everything that we pass to it it has to be a collection will be joined together separated by whatever we put in here so it doesn't have to be a whitespace it can be whatever you want but essentially everything that we pass to the Joint function um in in the collection that we pass to the Joint function will be joined together uh using this character or this string here as a separator so we're going to pass here a list comprehension to the Joint function which is going to say actually we don't need these uh square brackets we're going to say format the or of whatever character we're going to call it C now for C in message and that's basically all we need to do because what happens now is we take the message we iterate over the characters for each character in the message we're going to calculate the asky code we're going to format it oh by the way I forgot the B here we're going to format it into a binary format and the result will be joined together to a full string or all the results will be joined together to a full string where each character is separated by whites spaces so if we now go ahead and say print binary this would be the encoded message so we have a sequence of zeros and ones uh representing the message hello world this is my message with the exclamation marks with the wies spaces and everything um this is how you encode this now how do we decode this uh it's not too different it's basically almost the same but we need to use two different functions we need to use the character function and the integer function so basically the type casting functions uh or actually the the character function is more like the opposite of the or function so you take a number and you turn it into a character um so for that what we're going to do is we're going to say the binary text is going to be equal to and I'm going to do this manually even though we could just take this variable here so I would have to print binary first then I'm going to copy all of this here and we're going to take that here statically as the string so this is going to be our string we're going to delete all of this here so we only have that in our script now and in order to take this sequence of zeros and ones and turn it into a human readable format what we now do is we say normal equals and um maybe again before we do that let's discuss the functions individually if I use the function um character so CHR and I pass an integer to it so for example 32 and I print the result of this um okay in this case this is not something that is visible let's go 45 we see a dash let's go 51 uh we see a three What was a I don't know what a was okay but you can see what happens you basically pass um a number you pass an integer and it gives you the respective character so you pass an asky code and you get a character now I want to find some character here okay there you go 70 is f so you pass a number here and it turns it into a character and what we want to do now is we want to take all these values here turn them into character uh into characters and then append them or join them together again uh without wh space spes of course because each character unless the character itself is a whites space doesn't have to be separated by whites spaces obviously um the problem that we have here though is that we cannot just go ahead and pass this here to the character because that would be uh a million and uh 1,000 so that would not be interpreted as a binary number uh which is why we have to use the integer function as well so if we say int and then we pass to that integer uh some some binary sequence here and I then say comma 2 so this is the base then what happens is I get the number the decimal number that is the result of taking that and turning it into decimal so if you take this sequence of digits and you interpret them as binary digits uh then the result is 45 so all we need to do now to get the normal text back is we need to say normal equals empty string join because we don't want have any separators character of int of C2 for C in binary text because what we do now is or actually no this is not true sorry binary text Dot split on whitespace that is the the correct way so what we do here is we take the binary text we split everything in that uh string up based on the um white spaces so here we will make a cut here we will make a cut and so on so all these will be individual elements so the C here in the list comprehension will be one such thing here one such binary number and we're going to take that c we're going to feed it into the integer function to turn it into a decimal number we're going to take the decimal number feed it into the character function which is going to give us the character and then we're going to join all of this together um to a string and the result will be obviously hello world this is my message so this is how you convert text to Binary and binary to text in Python all right so that's it for today's video I hope you enjoyed it and hope you learned something if so let me know by hitting a like button and leaving a comment in the comment section down below and of course don't forget to subscribe to this Channel and hit the notification Bell to not miss a single future video for free other than that thank you much for watching see you on the next video and bye [Music]

Original Description

Today we learn how to convert text to binary and back in Python. ◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾ 📚 Programming Books & Merch 📚 🐍 The Python Bible Book: https://www.neuralnine.com/books/ 💻 The Algorithm Bible Book: https://www.neuralnine.com/books/ 👕 Programming Merch: https://www.neuralnine.com/shop 🌐 Social Media & Contact 🌐 📱 Website: https://www.neuralnine.com/ 📷 Instagram: https://www.instagram.com/neuralnine 🐦 Twitter: https://twitter.com/neuralnine 🤵 LinkedIn: https://www.linkedin.com/company/neuralnine/ 📁 GitHub: https://github.com/NeuralNine 🎙 Discord: https://discord.gg/JU4xr8U3dm 🎵 Outro Music From: https://www.bensound.com/ Timestamps: (0:00) Intro (0:22) Text to Binary (5:02) Binary to Text (9:07) Outro
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from NeuralNine · NeuralNine · 0 of 60

← Previous Next →
1 Visualizing Stock Data With Candlestick Charts in Python
Visualizing Stock Data With Candlestick Charts in Python
NeuralNine
2 Python Beginner Tutorial #1 - Installation and First Program
Python Beginner Tutorial #1 - Installation and First Program
NeuralNine
3 Python Beginner Tutorial #2 - Variables and Data Types
Python Beginner Tutorial #2 - Variables and Data Types
NeuralNine
4 Python Beginner Tutorial #3 - Operators and User Input
Python Beginner Tutorial #3 - Operators and User Input
NeuralNine
5 Python Beginner Tutorial #4 - If Statements and Conditions
Python Beginner Tutorial #4 - If Statements and Conditions
NeuralNine
6 Python Beginner Tutorial #5 - Loops
Python Beginner Tutorial #5 - Loops
NeuralNine
7 Python Beginner Tutorial #6 - Sequences and Collections
Python Beginner Tutorial #6 - Sequences and Collections
NeuralNine
8 Python Beginner Tutorial #7 - Functions
Python Beginner Tutorial #7 - Functions
NeuralNine
9 Python Beginner Tutorial #8 - Exception Handling
Python Beginner Tutorial #8 - Exception Handling
NeuralNine
10 Python Beginner Tutorial #9 - File Operations
Python Beginner Tutorial #9 - File Operations
NeuralNine
11 Python Beginner Tutorial #10 - String Functions
Python Beginner Tutorial #10 - String Functions
NeuralNine
12 Python Intermediate Tutorial #1 - Classes and Objects
Python Intermediate Tutorial #1 - Classes and Objects
NeuralNine
13 Python Intermediate Tutorial #2 - Inheritance
Python Intermediate Tutorial #2 - Inheritance
NeuralNine
14 Python Intermediate Tutorial #3 - Multithreading
Python Intermediate Tutorial #3 - Multithreading
NeuralNine
15 Python Intermediate Tutorial #4 - Synchronizing Threads
Python Intermediate Tutorial #4 - Synchronizing Threads
NeuralNine
16 Python Intermediate Tutorial #5 - Events and Daemon Threads
Python Intermediate Tutorial #5 - Events and Daemon Threads
NeuralNine
17 Python Intermediate Tutorial #6 - Queues
Python Intermediate Tutorial #6 - Queues
NeuralNine
18 Python Intermediate Tutorial #7 - Sockets and Network Programming
Python Intermediate Tutorial #7 - Sockets and Network Programming
NeuralNine
19 Python Intermediate Tutorial #8 - Database Programming
Python Intermediate Tutorial #8 - Database Programming
NeuralNine
20 Python Intermediate Tutorial #9 - Recursion
Python Intermediate Tutorial #9 - Recursion
NeuralNine
21 Python Intermediate Tutorial #10 - XML Processing
Python Intermediate Tutorial #10 - XML Processing
NeuralNine
22 Python Intermediate Tutorial #11 - Logging
Python Intermediate Tutorial #11 - Logging
NeuralNine
23 Python Data Science Tutorial #1 - Anaconda and PyCharm Setup
Python Data Science Tutorial #1 - Anaconda and PyCharm Setup
NeuralNine
24 Python Data Science Tutorial #2 - NumPy Arrays
Python Data Science Tutorial #2 - NumPy Arrays
NeuralNine
25 Python Data Science Tutorial #3 - Numpy Functions
Python Data Science Tutorial #3 - Numpy Functions
NeuralNine
26 Python Data Science Tutorial #4 - Plotting Functions With Matplotlib
Python Data Science Tutorial #4 - Plotting Functions With Matplotlib
NeuralNine
27 Python Data Science Tutorial #5 - Subplots and Multiple Windows
Python Data Science Tutorial #5 - Subplots and Multiple Windows
NeuralNine
28 Python Data Science Tutorial #6 - Matplotlib Styling
Python Data Science Tutorial #6 - Matplotlib Styling
NeuralNine
29 Python Data Science Tutorial #7 - Bar Charts with Matplotlib
Python Data Science Tutorial #7 - Bar Charts with Matplotlib
NeuralNine
30 Python Data Science Tutorial #8 - Pie Charts with Matplotlib
Python Data Science Tutorial #8 - Pie Charts with Matplotlib
NeuralNine
31 Python Data Science Tutorial #9 - Plotting Histograms with Matplotlib
Python Data Science Tutorial #9 - Plotting Histograms with Matplotlib
NeuralNine
32 Python Data Science Tutorial #10 - Scatter Plots with Matplotlib
Python Data Science Tutorial #10 - Scatter Plots with Matplotlib
NeuralNine
33 Python Data Science Tutorial #11 - 3D Plotting with Matplotlib
Python Data Science Tutorial #11 - 3D Plotting with Matplotlib
NeuralNine
34 Python Data Science Tutorial #12 - Pandas Series
Python Data Science Tutorial #12 - Pandas Series
NeuralNine
35 Python Data Science Tutorial #13 - Pandas Data Frames
Python Data Science Tutorial #13 - Pandas Data Frames
NeuralNine
36 Python Data Science Tutorial #14 - Pandas Statistics
Python Data Science Tutorial #14 - Pandas Statistics
NeuralNine
37 Python Data Science Tutorial #15 - Pandas Sorting and Functions
Python Data Science Tutorial #15 - Pandas Sorting and Functions
NeuralNine
38 Python Data Science Tutorial #16 - Pandas Merging Data Frames
Python Data Science Tutorial #16 - Pandas Merging Data Frames
NeuralNine
39 Python Data Science Tutorial #17 - Pandas Queries
Python Data Science Tutorial #17 - Pandas Queries
NeuralNine
40 Python Machine Learning Tutorial #1 - What is Machine Learning?
Python Machine Learning Tutorial #1 - What is Machine Learning?
NeuralNine
41 Python Machine Learning Tutorial #2 - Linear Regression
Python Machine Learning Tutorial #2 - Linear Regression
NeuralNine
42 Python Machine Learning Tutorial #3 - K-Nearest Neighbors Classification
Python Machine Learning Tutorial #3 - K-Nearest Neighbors Classification
NeuralNine
43 Python Machine Learning #4 - Support Vector Machines
Python Machine Learning #4 - Support Vector Machines
NeuralNine
44 Python Machine Learning Tutorial #5 - Decision Trees and Random Forest Classification
Python Machine Learning Tutorial #5 - Decision Trees and Random Forest Classification
NeuralNine
45 Python Machine Learning Tutorial #6 - K-Means Clustering
Python Machine Learning Tutorial #6 - K-Means Clustering
NeuralNine
46 Python Machine Learning Tutorial #7 - Neural Networks
Python Machine Learning Tutorial #7 - Neural Networks
NeuralNine
47 Python Machine Learning Tutorial #8 - Handwritten Digit Recognition with Tensorflow
Python Machine Learning Tutorial #8 - Handwritten Digit Recognition with Tensorflow
NeuralNine
48 Generating Poetic Texts with Recurrent Neural Networks in Python
Generating Poetic Texts with Recurrent Neural Networks in Python
NeuralNine
49 Stock Portfolio Visualization with Matplotlib in Python
Stock Portfolio Visualization with Matplotlib in Python
NeuralNine
50 Analyzing Coronavirus with Python (COVID-19)
Analyzing Coronavirus with Python (COVID-19)
NeuralNine
51 Making Text Images Readable Again with Python and OpenCV
Making Text Images Readable Again with Python and OpenCV
NeuralNine
52 Neural Networks Simply Explained (Theory)
Neural Networks Simply Explained (Theory)
NeuralNine
53 Motion Filtering with OpenCV in Python
Motion Filtering with OpenCV in Python
NeuralNine
54 Top 5 Programming Languages To Learn in 2020
Top 5 Programming Languages To Learn in 2020
NeuralNine
55 Simple TCP Chat Room in Python
Simple TCP Chat Room in Python
NeuralNine
56 Image Classification with Neural Networks in Python
Image Classification with Neural Networks in Python
NeuralNine
57 Edge Detection with OpenCV in Python
Edge Detection with OpenCV in Python
NeuralNine
58 S&P 500 Web Scraping with Python
S&P 500 Web Scraping with Python
NeuralNine
59 Simple Sentiment Text Analysis in Python
Simple Sentiment Text Analysis in Python
NeuralNine
60 Introduction - Algorithms & Data Structures #1
Introduction - Algorithms & Data Structures #1
NeuralNine

This video teaches how to convert text to binary and back in Python using built-in functions and list comprehension. It covers the concepts of text encoding, binary representation, and text decoding. By following the steps outlined in the video, viewers can learn how to write Python code for text to binary conversion and vice versa.

Key Takeaways
  1. Get ASCII code of individual characters using ord function
  2. Format ASCII code into binary using format function
  3. Combine binary values separated by white spaces using join function
  4. Use list comprehension to format binary string
  5. Use character function to turn integer into character
  6. Use integer function to turn binary string into decimal number
  7. Split the binary text into individual binary numbers using the split function on whitespace
  8. Feed each binary number into the int function to turn it into a decimal number
  9. Feed the decimal number into the chr function to get the corresponding character
  10. Join all the characters together into a string using the join function
💡 The video demonstrates how to use built-in Python functions and list comprehension to convert text to binary and back without relying on external modules or imports.

Related Reads

Up next
Claude Can Now Do EVERYTHING on Shopify
Brendan Gillen
Watch →