首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >如何将IEEE Python float转换为Microsoft Basic float

如何将IEEE Python float转换为Microsoft Basic float
EN

Stack Overflow用户
提问于 2010-02-16 02:47:40
回答 4查看 1.3K关注 0票数 3

我得到了Python的浮点值,我需要将它转换成Microsoft Basic float (MBF)格式。幸运的是,从互联网上得到了一些代码,可以做相反的事情。

代码语言:javascript
运行
复制
def fmsbin2ieee(self,bytes):
    """Convert an array of 4 bytes containing Microsoft Binary floating point
    number to IEEE floating point format (which is used by Python)"""
    as_int = struct.unpack("i", bytes)
    if not as_int:
        return 0.0
    man = long(struct.unpack('H', bytes[2:])[0])
    exp = (man & 0xff00) - 0x0200
    if (exp & 0x8000 != man & 0x8000):
        return 1.0
        #raise ValueError('exponent overflow')
    man = man & 0x7f | (man << 8) & 0x8000
    man |= exp >> 1
    bytes2 = bytes[:2]
    bytes2 += chr(man & 255)
    bytes2 += chr((man >> 8) & 255)
    return struct.unpack("f", bytes2)[0]

现在我需要逆转这个过程,但还没有成功。有什么需要帮忙的吗?

EN

回答 4

Stack Overflow用户

发布于 2010-02-16 03:21:11

如果你打算在Windows下运行时执行这些转换,更快的方法可能是下载并安装mbf2ieee.exe,然后调用结果Mbf2ieee.dll提供的CVS函数(例如通过ctype)。

如果你热衷于使用纯Python语言,我认为(但我不能测试,因为手头没有MBF数字),下面的代码可能会起作用(我刚把它从C code here移植到Python ):

代码语言:javascript
运行
复制
def mbf2ieee(mbf_4bytestring):
  msbin = struct.unpack('4B', mbf_4bytestring)
  if msbin[3] == 0: return 0.0

  ieee = [0] * 4
  sign = msbin[2] & 0x80
  ieee_exp = msbin[3] - 2
  ieee[3] = sign | (ieee_exp >> 1)
  ieee[2] = (ieee_exp << 7) | (msbin[2] & 0x7f)
  ieee[:2] = msbin[:2]

  return struct.unpack('f', ieee)[0]

如果这有问题,你能给出一些输入值和预期结果的例子吗?

编辑:如果它是你想要的反向函数,它应该是:

代码语言:javascript
运行
复制
def float2mbf4byte(f):
  ieee = struct.pack('f', f)
  msbin = [0] * 4
  sign = ieee[3] & 0x80

  msbin_exp = (ieee[3] << 1) | (ieee[2] >> 7)
  # how do you want to treat too-large exponents...?
  if msbin_exp == 0xfe: raise OverflowError
  msbin_exp += 2

  msbin[3] = msbin_exp
  msbin[2] = sign | (ieee[2] & 0x7f)
  msbin[:2] = ieee[:2]
  return msbin
票数 4
EN

Stack Overflow用户

发布于 2010-08-09 23:12:59

嗯,我尝试了float2mbf4byte(),做了两个修改:

  1. 转换负值现已生效,
  2. for Python 2,ieee应为整型列表,而不是字符串

代码片段:

代码语言:javascript
运行
复制
def float2mbf4byte(f):
    ieee = [ord(s) for s in struct.pack('f', f)]
    msbin = [0] * 4

    sign = ieee[3] & 0x80
    ieee[3] &= 0x7f

    msbin_exp = (ieee[3] << 1) | (ieee[2] >> 7)
    # how do you want to treat too-large exponents...?
    if msbin_exp == 0xfe: raise OverflowError
    msbin_exp += 2

    msbin[3] = msbin_exp
    msbin[2] = sign | (ieee[2] & 0x7f)
    msbin[:2] = ieee[:2]
    return msbin

def ieee2fmsbin(f):
    return struct.pack('4B', *float2mbf4byte(f))
票数 0
EN

Stack Overflow用户

发布于 2020-12-11 09:17:33

mbf2ieee无法正确转换,请尝试以下命令:

代码语言:javascript
运行
复制
import struct

def float2mbf4byte(f):
    ieee = struct.pack('f', f)
    msbin = [0] * 4
    sign = ieee[3] & 0x80

    msbin_exp = (ieee[3] << 1) | (ieee[2] >> 7)
    # how do you want to treat too-large exponents...?
    if msbin_exp == 0xfe: raise OverflowError
    msbin_exp += 2

    msbin[3] = msbin_exp
    msbin[2] = sign | (ieee[2] & 0x7f)
    msbin[:2] = ieee[:2]
    return msbin

def mbf2ieee(mbf_4bytestring):
    msbin = struct.unpack('4B', mbf_4bytestring)
    if msbin[3] == 0: return 0.0

    ieee = [0] * 4
    sign = msbin[2] & 0x80
    ieee_exp = msbin[3] - 2
    ieee[3] = sign | (ieee_exp >> 1)
    ieee[2] = (ieee_exp << 7) | (msbin[2] & 0x7f)
    ieee[:2] = msbin[:2]

    return struct.unpack('f', bytearray(ieee))[0]


print(mbf2ieee(bytearray(float2mbf4byte(52400126))))

..。你将会得到:

builtins.ValueError:字节必须在(0,256)范围内,因为正确答案是[0,E4,47,9A],但float2mbf4byte提供[0, 228, 19527, 76]。第3个元素不是字节大小值

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2268191

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档