Newbie lost(new info)

John Roth newsgroups at jhrothjr.com
Thu Feb 26 09:21:56 EST 2004


"Angelo Secchi" <secchi at sssup.it> wrote in message
news:mailman.150.1077786326.8594.python-list at python.org...
>
> John and Anthon thanks for your help.
> If I did it correctly this is the outcome of the code you asked me to
> try:
>
>
> >from binascii import hexlify
> >inf = file('foo','rb')
> >data = inf.read(1970)
> >for i in range(223):
> >     index = i * 4 + 113
> >     print hexlify(data[index: index + 4])
>
>
> 46ee3bb4 (I know that this should be 15612852)

Using the following little program:

-----------------------
import binascii
import struct

str1 = "46ee3bb4"
str2 = binascii.unhexlify(str1)
float = struct.unpack(">f", str2)
print float
float2 = struct.unpack("f", str2)
print float2
-----------------------------

I get:

-----------------------------
(30493.8515625,)
(-1.7502415516901237e-007,)
-----------------------------

So it's clearly *not* standard IEEE-488
format, and it's going to require some bit
twiddling to convert.

The last 3 bytes (in big-endian format)
are the fraction. I believe the first bit is
the fraction sign, and the next 7 bits are
the signed exponent in 4-bit chunks.
In other words, the exponent of your
normalized example is -6. The shift
quantity is in hex digits, not bits!

I've verified that this is the actual format
using the Windows Calc applet - handy
sucker when you want to do hex to decimal
conversions.

John Roth


> 00000000 (I know that this should be 0)
> 00000000 (I know that this should be 0)
> 465de39a (I know that this should be 6153114)
> 00000000 (I know that this should be 0)
> 00000000 (I know that this should be 0)
> ...
>
>
> John just to understand what you said, if the file is in IBM propietary
> binary format (EBCDIC ?) I cannot convert it in ASCII using Python?
>
> Thanks again
> Angelo
>
>
>
>
>
>
>
> On Wed, 25 Feb 2004 20:18:47 -0500
> "John Roth" <newsgroups at jhrothjr.com> wrote:
>
> >
> > "Anton Vredegoor" <anton at vredegoor.doge.nl> wrote in message
> > news:403d3b33$0$11500$3a628fcd at reader2.nntp.hccnet.nl...
> > > "John Roth" <newsgroups at jhrothjr.com> wrote:
> > >
> > > >For the rest of it, I'd like to see a *real* hex dump in mainframe
> > > >format. From what you've given us so far I'm not certain whether
> > > >the struct module can convert the data for you.
> > >
> > > Probably what John wants to see is the output of something like
> > > this:
> > >
> > > #one 'line' of data (since it's a binary file there is no real line
> > > #ending convention: a line is just a specific number of bytes long,
> > > #it's important to find out how many exactly)
> > >
> > > from binascii import hexlify
> > > inf = file('somefile','rb')
> > > data = inf.read(1005) #a 113 bytes string + 232 4 bytes floats
> > > L = map(hexlify,data)
> > > print L
> > >
> > > Anton
> >
> > Thank you! I wasn't aware of that module. It looks like it
> > should do exactly what's needed.
> >
> > Although this would probably do instead of the last 2 lines
> > (and excuse the fact that it's real ugly code, as well as
> > untested)
> >
> > for i in range(4):
> >     index = i * 32
> >     print hexlify(data[index: index+32])
> > for i in range(232):
> >     index = i * 4 + 113
> >     print hexlify(data[index: index + 4])
> >
> > John Roth
> > >
> > >
> >
> >
> > -- 
> > http://mail.python.org/mailman/listinfo/python-list
>
>
> --
> ========================================================
>  Angelo Secchi                     PGP Key ID:EA280337
> ========================================================
>   Current Position:
>   Graduate Fellow Scuola Superiore S.Anna
>   Piazza Martiri della Liberta' 33, Pisa, 56127 Italy
>   ph.: +39 050 883365
>   email: secchi at sssup.it www.sssup.it/~secchi/
> ========================================================
>





More information about the Python-list mailing list