- Code: Select all
#!/usr/bin/env python
# String, split into octets and left-padded with zeros.
str = "00101010 10001001 00100001 11100101 00111001"
# Create an array of the input
arr = str.split(' ')
# The processed integers will go in this list
numbers = []
# Each 8 characters (0 or 1) is considered an octet. We're going to loop through each one here.
for octet in arr:
# Set some temporary variables.
out = 0
multiplier = 128
# Loop through each character in a single octet.
for num in octet:
# "out" gets turned into the number times multiplier. The multiplier changes depending on the position in the string.
out += int(num) * multiplier
# Divide multiplier by 2 using bitwise arithmetic. Using multiplier = multiplier / 2 achieves the same effect.
# This works because shifting each bit right by 1 effectively divides by two.
multiplier = multiplier >> 1
numbers.append(out)
# Print it!
for number in numbers:
print chr(number),
To run this, you'd need Python. Just paste it into a new text file and run it with the interpreter (won't get into that here, but if you installed the Windows version, a double-click should work).