Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: how to reading binary data...

0 views
Skip to first unread message

mar...@nordesia.org

unread,
Oct 21, 2004, 4:07:56 AM10/21/04
to pytho...@python.org
## I hope this code could help you.

def readLong(fin):
"""Reads next long from file"""
s = fin.read(4)

if len(s) == 0: # test reaching of end of file
raise Exception("END-OF-FILE")
return str2long(s)

# First element of this list, is the index in a 4 byte string of the
most-significative-byte, second is the second most-significative and so
on.
SIGNIFICANCE = [0,1,2,3] # this list is useful when reading bigendian longs
#SIGNIFICANCE = [3,2,1,0] # this list is useful when reading littleendian
longs

def str2long(s):
"""Returns a long, using the first four byte of a string"""
l = 0L # the long we will get
for pos in SIGNIFICANCE: # get each byte in the order specified by the
significance list
if pos < len(s):
c = s[pos]
else: # if we take a byte that is not in byte string (this happens at
the end of file when file length is not multiple of 4.
c = chr(0)

l*=0x100 # shift left 8 bits
l+=ord(c) # add the readed byte at the LSB.

return l

#Testing
fi = open("name-of-file","rb")
run = 1
while run:
try:
print hex(readLong(fi))
except Exception:
run=0
fi.close()


Regards, marcos.

>
> Hello
>
> I opened any particular file with the 'rb' mode that is read binary mode.
> so python will treat the data as a raw data. now i want to read first 4
> bytes only then i will convert the first 4 bytes into long datatype and
> then again read 4 bytes and will do the same.
> But how to set & move the pointer using loop?
> also how to convert into long?
>
> Regards
> Sandeep
>
> --
> http://mail.python.org/mailman/listinfo/python-list

Miki Tebeka

unread,
Oct 21, 2004, 4:07:32 AM10/21/04
to Sandeep Avinash Gohad, pytho...@python.org
Hello Sandeep,

> I opened any particular file with the 'rb' mode that is read binary
> mode. so python will treat the data as a raw data. now i want to read
> first 4 bytes only then i will convert the first 4 bytes into long
> datatype and then again read 4 bytes and will do the same.
> But how to set & move the pointer using loop?

Each time you read from a file the "file pointer" is automatically
incrmented.

>>> f = open(".bashrc")
>>> f.tell()
0L
>>> f.read(1)
'#'
>>> f.tell()
1L
See http://docs.python.org/lib/bltin-file-objects.html

> also how to convert into long?

http://docs.python.org/lib/module-struct.html

HTH.
--
------------------------------------------------------------------------
Miki Tebeka <miki....@gmail.com>
http://tebeka.spymac.net
The only difference between children and adults is the price of the toys

Bengt Richter

unread,
Oct 21, 2004, 5:03:34 AM10/21/04
to
On Thu, 21 Oct 2004 10:07:32 +0200, Miki Tebeka <miki....@zoran.com> wrote:

>Hello Sandeep,
>
>> I opened any particular file with the 'rb' mode that is read binary
>> mode. so python will treat the data as a raw data. now i want to read
>> first 4 bytes only then i will convert the first 4 bytes into long
>> datatype and then again read 4 bytes and will do the same.
>> But how to set & move the pointer using loop?
>Each time you read from a file the "file pointer" is automatically
>incrmented.
>
>>>> f = open(".bashrc")
>>>> f.tell()
>0L
>>>> f.read(1)
>'#'
>>>> f.tell()
>1L
>See http://docs.python.org/lib/bltin-file-objects.html
>
>> also how to convert into long?
>http://docs.python.org/lib/module-struct.html
>
>HTH.

Beware of cooked input if you want to convert binary data, if you're not on unix.
E.g., windows: (tell seems to tell the binary truth, but read collapses \r\n to \n
unless you use 'rb' mode)

>>> open('linesofthree.txt','rb').read()
'012\r\n567\r\nABC\r\n'
>>> f = open('linesofthree.txt')
>>> while True:
... pos = f.tell()
... byt = f.read(1)
... print '[%s:%r]' % (pos, byt),
... if not byt: break
...
[0:'0'] [1:'1'] [2:'2'] [3:'\n'] [5:'5'] [6:'6'] [7:'7'] [8:'\n'] [10:'A'] [11:'B'] [12:'C'] [13
:'\n'] [15:'']
>>> f = open('linesofthree.txt','rb')
>>> while True:
... pos = f.tell()
... byt = f.read(1)
... print '[%s:%r]' % (pos, byt),
... if not byt: break
...
[0:'0'] [1:'1'] [2:'2'] [3:'\r'] [4:'\n'] [5:'5'] [6:'6'] [7:'7'] [8:'\r'] [9:'\n'] [10:'A'] [11
:'B'] [12:'C'] [13:'\r'] [14:'\n'] [15:'']
>>> print open('linesofthree.txt','rb').read()
012
567
ABC


Regards,
Bengt Richter

Peter Hansen

unread,
Oct 21, 2004, 6:59:43 AM10/21/04
to
mar...@nordesia.org wrote:
> SIGNIFICANCE = [0,1,2,3] # this list is useful when reading bigendian longs
> #SIGNIFICANCE = [3,2,1,0] # this list is useful when reading littleendian
> longs
>
> def str2long(s):
> """Returns a long, using the first four byte of a string"""
> l = 0L # the long we will get
> for pos in SIGNIFICANCE: # get each byte in the order specified by the
> significance list
> if pos < len(s):
> c = s[pos]
> else: # if we take a byte that is not in byte string (this happens at
> the end of file when file length is not multiple of 4.
> c = chr(0)
>
> l*=0x100 # shift left 8 bits
> l+=ord(c) # add the readed byte at the LSB.
>
> return l

Ditch this! It would be much easier, faster,
more maintainable to use the struct module:

struct.unpack('<l', four_byte_string)

This returns an integer from little-endian data, while
using >l assumes big-endian data.

-Peter

0 new messages