Binary file reads and format using Python struct.unpack

~\Documents\fullchip\python\mysite\webpages\templates\webpages\test.py.html
#!/usr/bin/python
# Program to read and format binary files.
# 'rb' - read binary mode
# Use of struct.unpack
# Read 64k of memory locations.
# Write only 3 X 256 byte pages.
# Calling functions from a class.

import sys
import re
import commands
import struct

class rbin:
    def file_op(self,rfile):
        self.readfile  = rfile
        self.wfile = open('loadmem.txt', 'w')
        for line in self.readfile.readlines():
                for cnt in range (0,4096,1):
                    x = cnt*16
                    test = line[x:x+16]
                    if (len(test) == 14):
                        magic = struct.unpack("<14b",test)
                        for i in range(0,14,1):
                                self.wfile.write("0x%X\n" % (magic[i]))
                    else:
                        magic = struct.unpack("<16b",test)
                        for i in range(0,16,1):
                            if ((x <= 498) | ((x <= 65520) & (x >= 65280))):
                                self.wfile.write("0x%X\n" % (magic[i]))

        self.readfile.close()
        self.wfile.close()

# Call Class
T = rbin()
# Call function from class
T.file_op(open('reg.bin','rb'))

LTE - 4G Wireless Technology

Digital fundamentals.

Interview Questions.

Following list of programs are discussed in this sec:

¶ File Operations: 

  1. Open and read file from within the python program as a single string. Command line arguments lists as string with ‘sys.argv’.
  2. Use of  glob module in Python code . Read file using classes, global variables and functions. Program to open and write file.
  3. Specify files to be written from the command line. File writes using classes and global variables

¶ Program to read file, filter text, conversion to HEX, conversion from Hexadecimal to Signed Magnitude.S Steps for conditional statements, converting string to hexadecimalvalue, strip of white space at end, converting 12 bit hexadecimal values to 12 bit signed values.

¶ Advanced python code to cover UNIX shell commands, read lines from a text file, ‘sysargv’ command line operations, use of class and multiple functions. 

¶ Write a program to generate diamond pattern.

¶ Implement Python function calls to different classes.

¶ Python read binary files, use of struct.unpack.

¶ Python error’s with complete code examples:-

  1. TypeError: __init__() takes exactly 2 arguments (1 given),
  2. TypeError: 'str' object is not callable, TypeError: not all arguments converted during string formatting,
  3. AttributeError: ** instance has no attribute **

Tutorials @fullchipdesign.com

Verilog Tutorial.

LTE Tutorial.

Memory Tutorial.

Hope you liked! this page. Don't forgot to access relevant previous and next sections with links below.