LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-02-2015, 09:44 PM   #1
simhumcon
Member
 
Registered: Dec 2013
Posts: 35

Rep: Reputation: Disabled
python read binary file into array


hello pros!
im quite a newbie in python. please help me out!

so i have a hex file, that is about 2821 sector size. i want to create an array for every 128 sectors. is there an example to do this?

what ive done so far is, i read the hex file containment and added a comma after every 2 characters, to separate the values

Code:
f = open(file_location, "rb")
imgData=f.read(1444352)
f.close()

filebyte = imgData.encode('hex')

def insert_newlines(string, every=2):
    lines = []
    for i in xrange(0, len(string), every):
        lines.append(string[i:i+every])
    return ','.join(lines)

content = insert_newlines(filebyte)

real_content = content.split(',')

print real_content
 
Old 07-02-2015, 10:05 PM   #2
dslackw
Member
 
Registered: Aug 2014
Location: Orestiada, GR
Distribution: Slackware 15.0 x86_64 -stable
Posts: 158

Rep: Reputation: 44
Code:
f = open("binary", "rb")
r = f.read()
f.close()

sector = []

c = 0
for s in r:
    sector.append(s)
    c += 1
    if c == 128:
        sector.append(",")
        c = 0

print sector
If I understand this might give you the idea.
 
Old 07-03-2015, 03:09 AM   #3
simhumcon
Member
 
Registered: Dec 2013
Posts: 35

Original Poster
Rep: Reputation: Disabled
hi! that is what exactly what im trying to do. now im stuck else where

so, now, i have a list of hex values that i want to pack using struct.pack. so here's what i did after reading from the hex file

Code:
import sys,getopt

filename = file_location
blocksize = 1444352

opts,args = getopt.getopt(sys.argv[1:],'f:b:')
for o,a in opts:
	if o == '-f':
		filename = a
	if o == '-b':
		blocksize = a

offset = 0
with open(filename,"rb") as f:
	block = f.read(blocksize)
	str = ""
	for ch in block:
		str += hex(ord(ch))+" "


sector = []
c = 0
for s in str.split(' ') :
    sector.append(s)
    c += 1
    if c == 128 :
        sector.append("")
        c = 0
        sector  = ', '.join(sector)
        print sector
        #struct.pack('B', sector)
        sector = sector.split(',')
        for items in sector[0:127] :
            items = int(items, 16)
            #print items

        struct.pack('B', sector)
        break


but i keep getting error message "struct error : cannot convert argument to integer"
but ive already converted to int
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Python - How to get text file data into an array on python. golmschenk Programming 4 11-11-2013 09:15 AM
write a file fo data array (with float values) in a binary format in Python justinlapin Programming 3 04-06-2012 04:35 AM
Reading binary file in Python pwc101 Programming 4 04-28-2010 04:01 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:11 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration