久久久久久久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 的意思不同)
主站蜘蛛池模板: 97视频在线观看免费 | 久草99| 亚洲综合视频 | 日本超碰| 日韩精品一区二区三区 | 亚洲精品久久久久中文字幕欢迎你 | 欧美簧片 | 久久久久久久99 | 91欧美| 伊人无码高清 | 国产探花在线观看视频 | 国产精品中文字幕在线 | 一区二区三区电影在线观看 | 亚洲黄色高清视频 | 亚洲精品福利在线 | 99精品国产一区二区青青牛奶 | 成人网av | 亚洲男女激情 | 国产一区二区三区在线视频 | 看av在线 | 亚洲精品视频二区 | 国产精产国品一二三产区视频 | 中日字幕大片在线播放 | 午夜视频在线 | 精品欧美一区二区三区久久久 | 久久99久久98精品免观看软件 | 久久精品一区二区 | 精品成人在线视频 | 精品国产区 | 丝袜毛片 | 免费看国产片在线观看 | 亚洲一区二区在线 | 综合国产在线 | 在线一区| 97精品国产97久久久久久免费 | 91色视频在线 | 中文字幕av在线 | 国产精品欧美精品日韩精品 | 2022精品国偷自产免费观看 | 午夜视频网 | 在线一级片 |