Dividing a 24-digit binary number to 3 equal parts

Question:

I’d like to know how can I divide a 24-digit binary number which is taken from user in python to 3 parts and then put different conditions on each of these 3 parts.

e.g: input => 111100011110110100100100

divide the input to 3 equal parts (each part should have 8 digits): 11110001 | 11101101 | 00100100
for the first part I want to turn the 8-digit bin num to an int num (no sign + -)
for the second part I want to turn the 8-digit bin num to an int num (with sign + -)
for the third part I want to take an ASCII output from the number in the 3rd part
So the output for the example above will be:

241
-109
$

I tried to make a list out of the input taken from the user and then get access to index for each divided part but I don’t know what I should do to have access to the exact 8-digits part of that input

  binaryNum = input()
    if len(binaryNum) < 24:
        print("error")
    binaryList = list(binaryNum)
Asked By: hAsTi a.r.m.y

||

Answers:

I think I managed to do it:

image of working code

My script saves the value as a list and as a string, b/c idk what you wanted the end value to be

  • Basically it takes the number
  • Splits it into 8bit parts (in a list)
  • Converts the first one to denary
  • Second one to denary using only the last 7bits
  • and makes it negative/positive as per first bit
  • Converts the third to denary then to the ASCII char
  • Returns this as either a list of a string

Hope this helps!

binaryInput = input('Input your binary number: ')

binaryList = [] #blank list
for i in range(0, 24, 8): #every 8th number
    binaryList.append(binaryInput[i:i+8]) #add the number and the next 7 numbers

returnList = [] #blank list

returnList.append(int(binaryList[0], 2)) #convert from base2 to base10

if binaryList[1][0] == '1': #if first char is a 1 (ie negative)
    returnList.append(-abs(int(binaryList[1][1:], 2))) #make denary out of last 7 chars then make it negative
elif binaryList[1][0] == '0':
    returnList.append(int(binaryList[1][1:], 2)) #just convert last 7 chars to denary

returnList.append(chr(int(binaryList[2], 2))) #convert base2 to base10 then base10 to char

for byte in returnList:
    print(byte) #prints each item separably

#returnString = '' #blank string
#for byte in returnList: #for each byte
    #returnString += str(byte) + ' ' #convert it to a string and add a space buffer to end
#print(returnString) #prints as a string

GitHub link to file

Answered By: Cornelius-Figgle