Home.Verilog.Digital Design.Digital Basics.Python.RF Basics.
Previous.
Next.
Custom Search

Legal Disclaimer

Chip Designing for ASIC/ FPGA Design engineers and Students
FULLCHIPDESIGN
Digital-logic Design...  Dream for many students… start learning front-end…

Legal Disclaimer

@TYH :- 4G LTE Long Term Evolution Tutorial, CloudComputing
PICS
Verilog Tutorial.
Get Noticed:- Submit your own content to be published on fullchipdesign.com

Send it to fullchip@gmail.com

Digital Basics Tutorial.
Binary file reads and format using Python struct.unpack
~\Desktop\rbin\rbin\read_binary.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'))
Download Input reg.bin file from here

Other reference codes

Open and read file as a single string.

Command line arguments lists as string with ‘sys.argv’. Glob module.

Classes, global variables and functions.

Open and write file. Generate diamond pattern.

Specify files to be written from the command line.

Writes using classes and global variables. Conditional statements, string to hexadecimal conversion,. Strip off white space at end. Convert 12 bit hexadecimal values to 12 bit signed values.. Conversion from Hexadecimal to Signed Magnitude. UNIX shell commands, read lines from a text file, ‘sysargv’ command line operations, use of class and multiple functions. 

Python error’s with complete code examples:- TypeError: __init__() takes exactly 2 arguments (1 given), TypeError: 'str' object is not callable,

TypeError: not all arguments converted during string formatting,

AttributeError:  ** instance has no attribute **