At 08:00 AM 1/6/2006, Thomas Heller wrote:

>RayS <[EMAIL PROTECTED]> writes:
>
>> I'm trying to find an efficient way to convert from DSP32C binary files
>> smmmmmmm mmmmmmmm mmmmmmmm eeeeeeee
>> to IEEE float
>> seeeeeee emmmmmmm mmmmmmmm mmmmmmmm
>>
>> I tried struct and bit-shifting from a C manual example, but it 
>> failed. Has someone else coded this in Python? I'll post the early 
>> attempt when I get into the office later, if not.
>>
>> I re-coded from scratch, converting each 32 bit ATT value to a binary 
>> string, slice-rearranging bits, then re-packing to IEEE. A bit 
>> slowwwww. At least they're small files.
>
>You could try ctypes bitfield structures, maybe, to access the fields?

Sounds interesting, but I have not used them; and on
http://starship.python.net/crew/theller/ctypes/tutorial.html
you wrote
"Bugs, ToDo and non-implemented things
Bitfields are not implemented."
Are they available? Documented?
I have __version__ = "0.9.6"

I also just read
http://publications.gbdirect.co.uk/c_book/chapter6/bitfields.html
which warned "Be careful using them. It can require a surprising amount of 
run-time code to manipulate these things and you can end up using more space 
than they save.
Bit fields do not have addresses­you can't have pointers to them or arrays of 
them."

My previous code from a C example, fast, but which does NOT return correct 
values!:
def DSP32ToIEEE(fh, address):
    """  bytes == 4 """
    res = ''
    fh.seek(address)
    b1 = struct.unpack("B",fh.read(1))[0]
    fh.seek(address+1)
    b2 = struct.unpack("B",fh.read(1))[0]
    fh.seek(address+2)
    b3 = struct.unpack("B",fh.read(1))[0]
    fh.seek(address+3)
    b4 = struct.unpack("B",fh.read(1))[0]
    if b4 & 0x80: ## check the sign bit
        b4 = b4 & 0x7f
        b2 = ~b2
        b3 = ~b3
        b4 = ~b4
        sign = -1
    else:
        b4 = b4 | 0x80
        sign = 1
    l4 = long(b4) << 16
    l3 = long(b3) << 8
    mantissa = (l4+l3+b2) / pow(2., 23)
    exp = pow(2., b1-127)
    VrmsSqd = sign*(mantissa*exp)/2.
    fh.seek(address)
    return VrmsSqd

Thanks,
Ray

_______________________________________________
Python-win32 mailing list
Python-win32@python.org
http://mail.python.org/mailman/listinfo/python-win32

Reply via email to