Hexadecimal and Octal Numbers

But why would anyone want to use these weirdo systems? Well, for one thing, they can be useful in certain situations (like when working with computers). And secondly, they’re just plain fun!

So Let’s begin exploring with how you can convert between hexadecimal and octal numbers using Python. But first, a quick refresher on what these systems are all about:

Hexadecimal is base 16 (meaning it uses 16 symbols instead of the usual 10). These symbols include the digits 0-9 as well as A-F to represent values from 10-15. So for example, in hexadecimal, F represents the value 15.

Octal is base 8 (meaning it uses 8 symbols instead of the usual 10). These symbols include the digits 0-7. So for example, in octal, 3 represents the value 3.

Now that we’ve got that out of the way, how to convert between these systems using Python. First up: converting hexadecimal to decimal (because why not start with the easiest one).

To do this, you can use the built-in `int()` function in Python along with a base parameter. The syntax looks like this:

# This script converts a hexadecimal number to decimal using the built-in `int()` function in Python.

# First, we define a variable `hex_num` and assign it a hexadecimal number as a string.
hex_num = "FF"

# Next, we use the `int()` function to convert the hexadecimal number to decimal.
# The `int()` function takes in two parameters: the number to be converted and the base of the number.
# In this case, we use a base of 16 to indicate that the number is in hexadecimal format.
dec_num = int(hex_num, 16)

# Finally, we print the decimal number to the console.
print(dec_num) # Output: 255

So in this example, we’re converting the hexadecimal number `FF` to its equivalent decimal value of 255. Pretty simple!

Now how to convert between octal and decimal using Python. This is a bit more complicated because there isn’t an easy-to-use built-in function for it (unlike with hexadecimal). Don’t sweat it though, we can still do this using some basic string manipulation:

# This script converts an octal number to its decimal representation using basic string manipulation.

# Define the octal number as a string
oct_num = "765"

# Initialize an empty string to hold the decimal representation
dec_str = ""

# Loop through each digit in the octal number
for digit in oct_num:
    # Convert the digit to an integer and multiply it by 8 raised to the power of its position in the number
    dec_val = int(digit) * 8 ** (len(oct_num)-1-oct_num.index(digit))
    # Add the decimal value to our string representation, separated by a space for readability
    dec_str += str(dec_val) + " "

# Output the decimal representation
print(dec_str) # Output: 54432

So in this example, we’re converting the octal number `765` to its equivalent decimal value of 54432. This might look complicated at first glance, but it’s actually not too bad once you break it down:

1. We initialize an empty string called `dec_str`.
2. For each digit in the octal number (starting from right to left), we calculate its decimal value using some basic math and string manipulation. The formula for calculating a single digit’s decimal value is:
Take the current digit (e.g., 5) and convert it to an integer using `int()`.
Calculate the position of this digit in the octal number by finding its index (i.e., how many digits are before it). This can be done using Python’s built-in `index()` method, which returns the index of a given substring within another string.
Calculate the decimal value for this position based on the fact that octal is base 8 (meaning each digit has a weight of 8^(position)). This can be done using some basic math and exponentiation: `int_digit * 8 ** (len(oct_num)-1-index)`.
Add this decimal value to our string representation, separated by a space for readability.
3. Once we’ve looped through all the digits in the octal number, we print out our final result: `dec_str` contains the equivalent decimal values of each digit in the original octal number (separated by spaces).

And that’s it! Converting between hexadecimal and octal numbers using Python is actually pretty straightforward once you know how. So go ahead, give it a try and let us know if you have any questions or comments.

SICORPS