# 浮点数的整数类型错误：找到非十六进制数字

``````def FloatToInt(num):
num = 22083.60066068796
print "FloatToInt:float:%.10f" %num
packed = struct.pack('!f', num)
print 'FloatToInt:Packed: %s' % repr(packed)
integers = [ord(c) for c in packed]
print 'FloatToInt:Integers: %s' % repr(integers)
val = int(0)
for i in integers:
val |= i
val = val << 8
return val

def IntToFloat(num):
#val = struct.unpack('!f', num.decode('hex'))[0]
#return val
intArr = []
while num > 0:
val = num & 0xFF
if val > 0:
intArr.append(int(val))
num = num >> 8
print 'IntToFloat:Integers: %s' % repr(intArr)
chars = [chr(c) for c in intArr]
str = ""
for ch in chars:
str += ch
print 'IntToFloat:Hex chars: %s' % repr(str)
ret = struct.unpack('!f', str.decode('hex'))[0]
return ret
``````

FloatToInt：float：22083.6006606880
FloatToInt：打包：'F \ xac \ x874'FloatToInt
：Integers：[

70，172，135，52 ] IntToFloat：Integers：[
52，135，172，70 ] IntToFloat：Hex字符：'4 \ x87 \ xacF'Traceback

（最近一次通话最近）：
........
TypeError：找到了非十六进制数字

`decode('hex')`用于解码这样的字符串`'0487AC0F'`

``````print( '0487AC0F'.decode('hex') )
``````

``````import struct

def FloatToInt(num):
num = 22083.60066068796
print "FloatToInt:float:%.10f" %num

packed = struct.pack('!f', num)
print 'FloatToInt:Packed: %s' % repr(packed)

integers = [ord(c) for c in packed]
print 'FloatToInt:Integers: %s' % repr(integers)

val = int(0)
for i in integers:
val |= i
val = val << 8

return val

def IntToFloat(num):
intArr = []

while num > 0:
val = num & 0xFF
if val > 0:
intArr.append(val)
num = num >> 8

# you have to reverse numbers
intArr = list(reversed(intArr))

print 'IntToFloat:Integers: %s' % repr(intArr)

text = "".join([chr(c) for c in intArr])
#chars = [chr(c) for c in intArr]
#text = ""
#for ch in chars:
#    text += ch

print 'IntToFloat:Hex chars: %s' % repr(text)

# you don't need decode('hex')
ret = struct.unpack('!f', text)[0]

return ret

num = 22083.60066068796
r = FloatToInt(num)
print '   FloatToInt:', r
r = IntToFloat(r)
print '   IntToFloat:', r
``````

• 单精度-称为“浮动”
• 双精度-称为“双精度”

``````import struct

num = 22083.60066068796
print "   float: %.10f" % num

packed = struct.pack('!d', num)
print '  packed: %s' % repr(packed)

ret = struct.unpack('!d', packed)[0]
print "unpacked: %.10f" %num
``````

``````   float: 22083.6006606880
packed: '@\xd5\x90\xe6q9\x86\xb2'
unpacked: 22083.6006606880
``````

`"double"` 在之前的代码中

`````` import struct

def FloatToInt(num):

print "FloatToInt:    float: %.10f" % num

packed = struct.pack('!d', num)
print 'FloatToInt:   Packed: %s' % repr(packed)

integers = [ord(c) for c in packed]
print 'FloatToInt: Integers: %s' % integers

val = int(0)
for i in integers:
val |= i
val = val << 8

print 'FloatToInt:   result: %d\n' % val

return val

def IntToFloat(num):

print 'IntToFloat:  integer: %d\n' % num

intArr = []

while num > 0:
val = num & 0xFF
if val > 0:
intArr.append(val)
num = num >> 8

intArr = list(reversed(intArr))

print 'IntToFloat: Integers: %s' % intArr

text = "".join([chr(c) for c in intArr])
print 'IntToFloat:Hex chars: %s' % repr(text)

ret = struct.unpack('!d', text)[0]
print 'IntToFloat:   result: %.10f\n' % ret

return ret

num = 22083.60066068796
r = FloatToInt(num)
r = IntToFloat(r)
``````

``````FloatToInt:    float: 22083.6006606880
FloatToInt:   Packed: '@\xd5\x90\xe6q9\x86\xb2'
FloatToInt: Integers: [64, 213, 144, 230, 113, 57, 134, 178]
FloatToInt:   result: 1195980674018107109888

IntToFloat:  integer: 1195980674018107109888
IntToFloat: Integers: [64L, 213L, 144L, 230L, 113L, 57L, 134L, 178L]
IntToFloat:Hex chars: '@\xd5\x90\xe6q9\x86\xb2'
IntToFloat:   result: 22083.6006606880
``````

0 条评论