Introduction
Conversions play a crucial role in various programming tasks, especially in fields like Electronics Engineering where numerical representations often need to be converted between different bases. Python provides powerful built-in functions and methods to perform these conversions efficiently. In this chapter, we'll explore how to convert numbers between decimal, binary, octal, and hexadecimal representations using Python. Additionally, we'll cover conversions between text and binary representations.
Index
- Decimal to Binary Conversion
- Binary to Decimal Conversion
- Decimal to Octal Conversion
- Octal to Decimal Conversion
- Decimal to Hexadecimal Conversion
- Hexadecimal to Decimal Conversion
- Text to Binary Conversion
- Binary to Text Conversion
Decimal to Binary Conversion
Converting decimal numbers to binary is a common task, particularly in electronics where binary is fundamental. Python provides the bin()
function to achieve this conversion.
decimal_number = 10
binary_representation = bin(decimal_number)
print(f"Binary representation of {decimal_number} is {binary_representation}")
Output:
Binary representation of 10 is 0b1010
Binary to Decimal Conversion
Converting binary numbers to decimal is equally important, especially when interpreting binary data received from sensors or devices. Python's int()
function with base 2 can be used for this conversion.
binary_number = "1010"
decimal_representation = int(binary_number, 2)
print(f"Decimal representation of {binary_number} is {decimal_representation}")
Output:
Decimal representation of 1010 is 10
Decimal to Octal Conversion
In certain scenarios, octal representation is preferred, particularly in electronics where it's used to represent bit flags or permissions. Python's oct()
function facilitates decimal to octal conversion.
decimal_number = 20
octal_representation = oct(decimal_number)
print(f"Octal representation of {decimal_number} is {octal_representation}")
Output:
Octal representation of 20 is 0o24
Octal to Decimal Conversion
Converting octal numbers back to decimal is straightforward using Python's int()
function with base 8.
octal_number = "24"
decimal_representation = int(octal_number, 8)
print(f"Decimal representation of {octal_number} is {decimal_representation}")
Output:
Decimal representation of 24 is 20
Decimal to Hexadecimal Conversion
Hexadecimal representation is commonly used in electronics for memory addresses and color codes. Python's hex()
function converts decimal to hexadecimal.
decimal_number = 255
hexadecimal_representation = hex(decimal_number)
print(f"Hexadecimal representation of {decimal_number} is {hexadecimal_representation}")
Output:
Hexadecimal representation of 255 is 0xff
Hexadecimal to Decimal Conversion
Converting hexadecimal back to decimal can be done using Python's int()
function with base 16.
hexadecimal_number = "ff"
decimal_representation = int(hexadecimal_number, 16)
print(f"Decimal representation of {hexadecimal_number} is {decimal_representation}")
Output:
Decimal representation of ff is 255
Text to Binary Conversion
Converting text to binary is essential in various applications such as encryption and data transmission. Python provides the encode()
method to convert text to binary.
text = "Hello, World!"
binary_representation = "".join(format(ord(char), "08b") for char in text)
print(f"Binary representation of '{text}' is {binary_representation}")
Output:
Binary representation of 'Hello, World!' is 0100100001100101011011000110110001101111001000000101011101101111011100100110110001100100
Binary to Text Conversion
Converting binary back to text is necessary for decoding transmitted binary data. Python offers the chr()
function to convert binary to text.
binary_data = "0100100001100101011011000110110001101111001000000101011101101111011100100110110001100100"
text_representation = "".join(chr(int(binary_data[i:i+8], 2)) for i in range(0, len(binary_data), 8))
print(f"Text representation of '{binary_data}' is '{text_representation}'")
Output:
Text representation of '0100100001100101011011000110110001101111001000000101011101101111011100100110110001100100' is 'Hello, World!'
Conclusion
Python offers convenient built-in functions for converting numbers between different bases, as well as between text and binary representations. These functionalities make it easy to handle various numerical and textual data in Electronics Engineering and other fields.
Top comments (0)