久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

Python中浮點數的二進制表示(位不是十六進制)

Binary representation of float in Python (bits not hex)(Python中浮點數的二進制表示(位不是十六進制))
本文介紹了Python中浮點數的二進制表示(位不是十六進制)的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

如何將字符串作為 32 位浮點數的二進制 IEEE 754 表示?

How to get the string as binary IEEE 754 representation of a 32 bit float?

示例

1.00 -> '00111111100000000000000000000000'

1.00 -> '00111111100000000000000000000000'

推薦答案

您可以使用 struct 包來做到這一點:

You can do that with the struct package:

import struct
def binary(num):
    return ''.join('{:0>8b}'.format(c) for c in struct.pack('!f', num))

將其打包為網絡字節序浮點數,然后將每個生成的字節轉換為 8 位二進制表示并將它們連接起來:

That packs it as a network byte-ordered float, and then converts each of the resulting bytes into an 8-bit binary representation and concatenates them out:

>>> binary(1)
'00111111100000000000000000000000'

編輯:有人要求擴大解釋.我將使用中間變量對每個步驟進行注釋.

Edit: There was a request to expand the explanation. I'll expand this using intermediate variables to comment each step.

def binary(num):
    # Struct can provide us with the float packed into bytes. The '!' ensures that
    # it's in network byte order (big-endian) and the 'f' says that it should be
    # packed as a float. Alternatively, for double-precision, you could use 'd'.
    packed = struct.pack('!f', num)
    print 'Packed: %s' % repr(packed)

    # For each character in the returned string, we'll turn it into its corresponding
    # integer code point
    # 
    # [62, 163, 215, 10] = [ord(c) for c in '>xa3xd7
']
    integers = [ord(c) for c in packed]
    print 'Integers: %s' % integers

    # For each integer, we'll convert it to its binary representation.
    binaries = [bin(i) for i in integers]
    print 'Binaries: %s' % binaries

    # Now strip off the '0b' from each of these
    stripped_binaries = [s.replace('0b', '') for s in binaries]
    print 'Stripped: %s' % stripped_binaries

    # Pad each byte's binary representation's with 0's to make sure it has all 8 bits:
    #
    # ['00111110', '10100011', '11010111', '00001010']
    padded = [s.rjust(8, '0') for s in stripped_binaries]
    print 'Padded: %s' % padded

    # At this point, we have each of the bytes for the network byte ordered float
    # in an array as binary strings. Now we just concatenate them to get the total
    # representation of the float:
    return ''.join(padded)

還有幾個例子的結果:

>>> binary(1)
Packed: '?x80x00x00'
Integers: [63, 128, 0, 0]
Binaries: ['0b111111', '0b10000000', '0b0', '0b0']
Stripped: ['111111', '10000000', '0', '0']
Padded: ['00111111', '10000000', '00000000', '00000000']
'00111111100000000000000000000000'

>>> binary(0.32)
Packed: '>xa3xd7
'
Integers: [62, 163, 215, 10]
Binaries: ['0b111110', '0b10100011', '0b11010111', '0b1010']
Stripped: ['111110', '10100011', '11010111', '1010']
Padded: ['00111110', '10100011', '11010111', '00001010']
'00111110101000111101011100001010'

這篇關于Python中浮點數的二進制表示(位不是十六進制)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Python 3 Float Decimal Points/Precision(Python 3 浮點小數點/精度)
Converting Float to Dollars and Cents(將浮點數轉換為美元和美分)
What are some possible calculations with numpy or scipy that can return a NaN?(numpy 或 scipy 有哪些可能的計算可以返回 NaN?)
Python float to ratio(Python浮動比率)
How to manage division of huge numbers in Python?(如何在 Python 中管理大量數字的除法?)
mean from pandas and numpy differ(pandas 和 numpy 的意思不同)
主站蜘蛛池模板: 久久久国产精品入口麻豆 | 暖暖日本在线视频 | 精品国产一区二区三区性色av | 午夜影院中文字幕 | 精品一区二区免费视频 | 午夜性色a√在线视频观看9 | av在线电影网站 | 日韩一级精品视频在线观看 | 一区二区三区四区在线视频 | 精品入口麻豆88视频 | 97色在线视频 | 懂色av一区二区三区在线播放 | 亚洲三级在线观看 | 成人在线看片 | 久久久女女女女999久久 | 欧美一级二级在线观看 | 久久免费香蕉视频 | av网站观看 | 久久在视频 | h视频在线播放 | 亚洲精品久久久9婷婷中文字幕 | 日韩中文字幕区 | a精品视频 | 久久er99热精品一区二区 | 天堂网中文字幕在线观看 | 国产精品99精品久久免费 | 欧美一级二级三级视频 | 日本久久精品 | 日韩av一区二区在线 | 人人操日日干 | 亚洲一区二区三区福利 | 在线久草| 欧美free性 | 亚洲 欧美 日韩 在线 | 在线三级电影 | 亚洲国产精品一区二区久久 | 成人午夜 | 精品视频在线免费观看 | av免费网站在线观看 | 国产免费自拍 | 欧美一区二区大片 |