1

I read from a hex file that's 1444352kB of size. I take 128 bytes of data and try to pack them using python struct.pack.

Below is the code :

#!/usr/bin/env python
import os
import struct
import ctypes
import array
import binascii

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
        #print sector
        sector  = ', '.join(sector)
        #print sector
        print type(sector)
        sector = sector.split(',')
        secdata = []
        for items in sector[0:127] :
            secdata.append(items)
        secdata2 = ','.join(secdata)
        print secdata2
        print type(secdata2)
        struct.pack('B', secdata2)
        break

The secdata that appears to be a list, I have converted to string. but I always get error struct.error: cannot convert argument to integer when I try to pack the 128 bytes of data.

user3148235
  • 21
  • 2
  • 4
  • Please [edit] your question and add the *full* traceback. –  Jul 06 '15 at 05:40
  • [`struct.pack`](https://docs.python.org/3.4/library/struct.html#struct.pack) takes a format and a series of values and converts that into a string. Your format asked `pack` to convert the (single) value from unsigned char ('B') to a string, so your input must be an unsigned char (equivalent to an integer). –  Jul 06 '15 at 05:44
  • It's unclear what you're trying to achieve: *how* do you want to pack your data? Perhaps you want to unpack things instead, since your `secdata2` appears to be a string, or a list of strings? –  Jul 06 '15 at 05:45
  • hello there! thanks for looking into this! so my secdata2 appears to be a string. like this : 0x50, 0xaa, 0x31, 0x0, 0x51.......128bytes. how do i pack these please? – user3148235 Jul 06 '15 at 05:49
  • what im trying to achieve here is to get 128 bytes from the hex file, and pack them using python struct.pack() – user3148235 Jul 06 '15 at 06:02
  • Once you've packed the bytes, what do you want to do with it? You don't assign the result of struct.pack() to anything. –  Jul 06 '15 at 06:04
  • I think you want to *unpack* your bytes; please have a careful look at the [examples](https://docs.python.org/3.4/library/struct.html#examples) given in the documentation. –  Jul 06 '15 at 06:05
  • And perhaps [this SO question](http://stackoverflow.com/questions/209513/convert-hex-string-to-int-in-python) can help you achieve what you want while not using `struct`. –  Jul 06 '15 at 06:12
  • can please send me show some example? im really new to python and its hard for me to understand the docs. i just want to pack a string that contains 128bytes – user3148235 Jul 07 '15 at 02:11

0 Answers0