Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Problem 4: Python programming-bin to hex Write a python program which converts e

ID: 3746493 • Letter: P

Question

Problem 4: Python programming-bin to hex Write a python program which converts each line of 32-bit binary into hex format. The python program should read from a text file of MIPS machine code (similar to the output file MIPS_machine code.txt of your assembler in homework 1), and should output another text file (for example, MIPS_ machine code_hex.txt) containing each instruction in hex form (for example, 0x200F0003). Attach: 1) your code, 2) a screenshot of its runtime output demonstrating its functionality, and 3) the input and output files - you should design the input file to demonstrate the program's functionality.

Explanation / Answer

#MIPS_machine_code.txt must be in the same directory as this python file

#code

# functin to convert integer in hex

def int2hex(num):

return hex(num)

# adjust name of input file and output file here

inputFile = "MIPS_machine_code.txt"

outputFile = "MIPS_machine_code_hex.txt"

with open(inputFile,"r") as f:

print("opened input file ")

with open(outputFile,'w') as of:

print("opened output file ")

# in case of one line containing only one 32 bit binary number

for wordb in f:

print("binary : "+wordb)

wordi = int(wordb,2)

wordh = int2hex(wordi)

print("hex : "+wordh+" ")

of.write(wordh+" ")

# in case of file containing single line on continuos binary

# wordb = f.read(32)

# while wordb != "":

#print("binary : "+wordb)

# wordi = int(wordb,2)

# wordh = int2hex(wordi)

# print("hex"+wordh+" ")

# of.write(wordh+" ")

# wordb = f.read(32)

f.close()

of.close()

#code ends here

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote