Question

In: Computer Science

PYTHON: How do I split up the binary string into chunks of 8 so that they...

PYTHON: How do I split up the binary string into chunks of 8 so that they are converted from binary to ASCII characters? For example "0011101000101001" would be eventually converted to ":)" . The code I have below is what I currently have. It currently only works for binary strings of 8. argv[1] is the text file it is reading from and argv[2] is basically just 8.

import sys
filename=sys.argv[1]
length = int(sys.argv[2])
message_data = open(filename, "r")
message_text = message_data.read()
if len(message_text) == 0:
sys.exit(1)
a= message_text.replace(',','')
a= ''.join(a.split())
conversion_list =[128, 64, 32, 16, 8, 4, 2, 1]
dec=0
for position, digit in enumerate((a)):
dec += conversion_list[position]* int(digit)
print("Decimal is ",dec)
e=chr(dec)

Solutions

Expert Solution

import sys
# it will split string with into give length
# and return list of string
def splitString(s, length):
return list((s[0+i:length+i] for i in range(0, len(s), length)))

filename=sys.argv[1]
length = int(sys.argv[2])

message_data = open(filename, "r")
message_text = message_data.read()

if len(message_text) == 0:
   sys.exit(1)

a= message_text.replace(',','')
# get splited list
a= splitString(a, length)
# you can uncomment it to see the list of splited string
# print(a)

conversion_list =[128, 64, 32, 16, 8, 4, 2, 1]

for i in range(len(a)):
   dec = 0 # assume initial decimal equuvalent is zero
   # scan the entire 8 length str
   for j in range(len(a[i])):
       # getting equivalent decimal
       dec = dec + conversion_list[j]*int(a[i][j])
   # printing equivalent decimal
   # you can uncomment these to see decimal value
   # print("Decimal is ",dec)
   e=chr(dec)
   print(e, end="")
print()


Related Solutions

how do I split this string in python?? testString="\0xdc\0x02\0xfe\0x7d\0xac" I want it to be recognized as...
how do I split this string in python?? testString="\0xdc\0x02\0xfe\0x7d\0xac" I want it to be recognized as having a data length of 5 and for the first value to be 0xdc, second one to be 0x02 and so on...
So I have a lab practical coming up; how do I know when to take an...
So I have a lab practical coming up; how do I know when to take an IR spec versus making a TLC chamber? Follow up question: Can someone walk me through the interpretation of a TLC chamber?
How do I write a script for this in python in REPL or atom, NOT python...
How do I write a script for this in python in REPL or atom, NOT python shell Consider the following simple “community” in Python . . . triangle = [ ["top", [0, 1]], ["bottom-left", [0, 0]], ["bottom-right", [2, 0]], ] This is the calling of function. >>> nearestneighbor([0, 0.6], triangle, myeuclidean) 'top' The new point is (0, 0.6) and the distance function is Euclidean. Now let’s confirm this result . . . >>> myeuclidean([0, 0.6], [0, 1]) 0.4 >>> myeuclidean([0,...
Python: How would I modify the class below that takes a string and returns an object...
Python: How would I modify the class below that takes a string and returns an object holding a valid NANP phone number. I am asked to filll in the three methods listed, but underfined, below: __str__(), area_code(), and normalize(). My task is to clean up differently formatted telephone numbers by removing punctuation, such as '(', '-', and the like, and removing and the country code (1) if present. I am asked to start by stripping non-digits, and then see if...
Python: If I am given a string consisting only of parentheses - (, ), {, },...
Python: If I am given a string consisting only of parentheses - (, ), {, }, [, and ], how would I write a Boolean function that takes a string and decides if it consists of valid nested parenthesis? One of the hints given was to use stack. For example --> {()[{}]}' is valid.
i have an array of strings, how do i sort them into a binary tree? C...
i have an array of strings, how do i sort them into a binary tree? C Program
how to do the tf-idf vectorization in python ? how do we fit the model? i...
how to do the tf-idf vectorization in python ? how do we fit the model? i am trying like this but i am getting confused please explain with complete program vectorizer = Tfidvectorizer() x=vectorizer.fit_transofrm() // what data should be passed here ? do we split it in tokens ?? do we use vocabulary?? do we use s.split() ?? x.toarray() ? == pleae provide complete program with output because i am not understanding
What is (567.12) octal in binary. What is(5CF.AD) hexadecimal in binary. Please show work so I...
What is (567.12) octal in binary. What is(5CF.AD) hexadecimal in binary. Please show work so I can understand how to solve thanks.
How do I Install a Python module that is not included with the Anaconda Distribution.
How do I Install a Python module that is not included with the Anaconda Distribution.
So how do sailboats sail up wind? It is obvious how they move with the wind...
So how do sailboats sail up wind? It is obvious how they move with the wind but definitely not so obvious how they go against the wind. Going along with this, baseballs would still fly even if there was no lift but how do frisbees and boomerangs work?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT