Re: Question about binary file reading

2009-03-05 Thread vibgyorbits
On Mar 5, 9:24 am, Marco Mariani wrote: > vibgyorbits wrote: > > l=map(lambda x: '%02x' %ord(x),d) > > s=string.join(l,sep='') > > > PS#. Endedup learning little bit of Lambda functions. :-) > > That's so 2007... > > The 2.5-esque way to write that is > > s = ''.join('%02x' % ord(x) for x in d) Y

Re: Question about binary file reading

2009-03-05 Thread Marco Mariani
vibgyorbits wrote: l=map(lambda x: '%02x' %ord(x),d) s=string.join(l,sep='') PS#. Endedup learning little bit of Lambda functions. :-) That's so 2007... The 2.5-esque way to write that is s = ''.join('%02x' % ord(x) for x in d) -- http://mail.python.org/mailman/listinfo/python-list

Re: Question about binary file reading

2009-03-05 Thread vibgyorbits
k,thanks all. l=map(lambda x: '%02x' %ord(x),d) s=string.join(l,sep='') PS#. Endedup learning little bit of Lambda functions. :-) Scott David Daniels <<< Thanks for your wisdom about the "spaces". Its a 3 liner code-snippet! -- http://mail.python.org/mailman/listinfo/python-list

Re: Question about binary file reading

2009-03-04 Thread Hendrik van Rooyen
"Benjamin Peterson" wrote: >So called encodings like "hex" and "rot13" are abuse of >encode() method. encode() should translate >between byte strings and unicode, not preform >transformations like that. This has been removed >in 3.x, so you should use binascii. When all else fails, and just for

Re: Question about binary file reading

2009-03-04 Thread Tino Wildenhain
Benjamin Peterson wrote: John Machin lexicon.net> writes: On Mar 5, 12:13 pm, Benjamin Peterson wrote: import binascii print binascii.hexlify(some_bytes) AFAICT binascii.hexlify(some_bytes) gives the SAME result as some_bytes.encode("hex") for much more typing -- I see no "better" here. So

Re: Question about binary file reading

2009-03-04 Thread Benjamin Peterson
John Machin lexicon.net> writes: > On Mar 5, 12:13 pm, Benjamin Peterson wrote: > > > > import binascii > > print binascii.hexlify(some_bytes) > > AFAICT binascii.hexlify(some_bytes) gives the SAME result as > some_bytes.encode("hex") for much more typing -- I see no > "better" > here. So calle

Re: Question about binary file reading

2009-03-04 Thread John Machin
On Mar 5, 12:13 pm, Benjamin Peterson wrote: > Tino Wildenhain wildenhain.de> writes: > > > Rhodri James wrote: > > > for b in x: > > >     print hex(ord(b)) > > > better: > > > print x.encode("hex") > > even better: > > import binascii > print binascii.hexlify(some_bytes) AFAICT binascii.hexlif

Re: Question about binary file reading

2009-03-04 Thread Scott David Daniels
vibgyorbits wrote: I'm writing a tool to do some binary file comparisons. I'm opening the file using fd=open(filename,'rb') > # Need to seek to 0x80 (hex 80th) location fd.seek(0x80) # Need to read just 8 bytes and get the result back in hex format. x=fd.read(8) print x This print

Re: Question about binary file reading

2009-03-04 Thread Benjamin Peterson
Tino Wildenhain wildenhain.de> writes: > Rhodri James wrote: > > for b in x: > > print hex(ord(b)) > > > > better: > > print x.encode("hex") even better: import binascii print binascii.hexlify(some_bytes) -- http://mail.python.org/mailman/listinfo/python-list

Re: Question about binary file reading

2009-03-04 Thread John Machin
On Mar 5, 10:51 am, "Rhodri James" wrote: > On Wed, 04 Mar 2009 23:28:32 -, Tino Wildenhain   > wrote: > > > > > Rhodri James wrote: > >> On Wed, 04 Mar 2009 22:58:38 -, vibgyorbits   > >> wrote: > > >>> I'm writing a tool to do some binary file comparisons. > >>> I'm opening the file us

Re: Question about binary file reading

2009-03-04 Thread Ben Finney
I just found a well-hidden part of the behaviour you expected. vibgyorbits writes: > # Need to read just 8 bytes and get the result back in hex format. > x=fd.read(8) > print x Why would this print the bytes in hex format? “Convert to hexadecimal” is not the default text encoding for ‘print’.

Re: Question about binary file reading

2009-03-04 Thread Ben Finney
vibgyorbits writes: > I'm writing a tool to do some binary file comparisons. > I'm opening the file using > > fd=open(filename,'rb') > > # Need to seek to 0x80 (hex 80th) location > > fd.seek(0x80) > > # Need to read just 8 bytes and get the result back in hex format. > x=fd.read(8) > print x

Re: Question about binary file reading

2009-03-04 Thread Rhodri James
On Wed, 04 Mar 2009 23:28:32 -, Tino Wildenhain wrote: Rhodri James wrote: On Wed, 04 Mar 2009 22:58:38 -, vibgyorbits wrote: I'm writing a tool to do some binary file comparisons. I'm opening the file using fd=open(filename,'rb') # Need to seek to 0x80 (hex 80th) location fd

Re: Question about binary file reading

2009-03-04 Thread Tino Wildenhain
Rhodri James wrote: On Wed, 04 Mar 2009 22:58:38 -, vibgyorbits wrote: I'm writing a tool to do some binary file comparisons. I'm opening the file using fd=open(filename,'rb') # Need to seek to 0x80 (hex 80th) location fd.seek(0x80) # Need to read just 8 bytes and get the result back i

Re: Question about binary file reading

2009-03-04 Thread Rhodri James
On Wed, 04 Mar 2009 22:58:38 -, vibgyorbits wrote: I'm writing a tool to do some binary file comparisons. I'm opening the file using fd=open(filename,'rb') # Need to seek to 0x80 (hex 80th) location fd.seek(0x80) # Need to read just 8 bytes and get the result back in hex format. x=fd.re

Re: Question about binary file reading

2009-03-04 Thread Chris Rebert
On Wed, Mar 4, 2009 at 2:58 PM, vibgyorbits wrote: > I'm writing a tool to do some binary file comparisons. > I'm opening the file using > > fd=open(filename,'rb') > > # Need to seek to 0x80 (hex 80th) location > > fd.seek(0x80) > > # Need to read just 8 bytes and get the result back in hex format

Question about binary file reading

2009-03-04 Thread vibgyorbits
I'm writing a tool to do some binary file comparisons. I'm opening the file using fd=open(filename,'rb') # Need to seek to 0x80 (hex 80th) location fd.seek(0x80) # Need to read just 8 bytes and get the result back in hex format. x=fd.read(8) print x This prints out garbage. I would like to kno