推荐学习书目
› Learn Python the Hard Way
Python Sites
› PyPI - Python Package Index
› http://diveintopython.org/toc/index.html
› Pocoo
值得关注的项目
› PyPy
› Celery
› Jinja2
› Read the Docs
› gevent
› pyenv
› virtualenv
› Stackless Python
› Beautiful Soup
› 结巴中文分词
› Green Unicorn
› Sentry
› Shovel
› Pyflakes
› pytest
Python 编程
› pep8 Checker
Styles
› PEP 8
› Google Python Style Guide
› Code Style from The Hitchhiker's Guide
ericbize
V2EX  ›  Python

请教 Python 二进制转 10 进制

  •  
  •   ericbize · Sep 29, 2017 · 5661 views
    This topic created in 3330 days ago, the information mentioned may be changed or developed.
    因为原创数据是感应器输出的, 00110101, 这样的东西,然后问题是 用 int ("00110101",2 ) 是可以得到 53 的,但是 用这个转吗方法需要 string 格式,但是 只要 string 就会 把 01 开头的默认为 8 进制。 那么要怎么样才能愉快的转成 10 进制 ?
    32 replies  •  2017-10-02 11:28:08 +08:00
    Cooky
    Cooky
    Sep 29, 2017 via Android
    1
    自问自答系列
    ericbize
    ericbize
    OP
    Sep 29, 2017
    2
    @Cooky 没有 ,a = 00110101 , print str(a) 得到的是 36929 , 我不知道怎么解决这个问题
    wwqgtxx
    wwqgtxx
    Sep 29, 2017
    3
    a = 0b00110101
    pright
    pright
    Sep 29, 2017❤️1
    4
    oct2dec = lambda x: (((x>>21)&0x7)<<7)|(((x>>18)&0x7)<<6)|(((x>>15)&0x7)<<5)|(((x>>12)&0x7)<<4)|(((x>>9)&0x7)<<3)|(((x>>6)&0x7)<<2)|(((x>>3)&0x7)<<1)|(x&0x7)

    a=00110101
    print oct2dec(a)
    ericbize
    ericbize
    OP
    Sep 29, 2017
    5
    @wwqgtxx 在数组加入 0b 的时候已经强行转了~~~
    ericbize
    ericbize
    OP
    Sep 29, 2017
    6
    @pright 谢谢你
    oaix
    oaix
    Sep 29, 2017
    7
    你是不是要实现这个效果
    ```
    >>> int(oct(00110101), 2)
    53
    ```
    oaix
    oaix
    Sep 29, 2017
    8
    感觉你的需求有点搞笑, 最初的那个八进制数字是怎么来的? eval 吗
    ericbize
    ericbize
    OP
    Sep 29, 2017
    9
    @oaix https://akizukidenshi.com/download/ds/aosong/DHT11.pdf 这个是说明, 就是一个 sensor 输出的数据。 其实网上是有其他计算方法的,但是我不想用,毕竟要查重,而且我答应过老师我自己写 code 不回 copy
    ltux
    ltux
    Sep 29, 2017 via Android
    10
    你的传感器输出二进制串 00110101 结果居然不是字符串也不是其二进制结果的整数,而是把这个二进制串按八进制理解得到的整数??? 233333 接口驱动谁写的,去打死他
    ltux
    ltux
    Sep 29, 2017 via Android
    11
    我觉得你传感器的输出简直不可思议……你说了半天都没说明白 python 接收到的到底是啥,是个 01 数组?字符串?整数?
    ltux
    ltux
    Sep 29, 2017 via Android❤️1
    12
    “这样的东西”是个啥呦……进制转换简直不算个问题,没学过编程的教一遍也能学会,但你得先把接收到的数据格式描述清楚喽
    ltux
    ltux
    Sep 29, 2017 via Android❤️1
    13
    如果 a 是个整数,a=0o00110101,则 int('%o'%(a),2) 即所求
    如果 a 是个 01 数组,a=[0,0,1,1,0,1,0,1],则 int(''.join(map(str,a)),2) 即所求
    msg7086
    msg7086
    Sep 30, 2017
    14
    你还是先说清楚到底是个什么东西吧。

    数字本来就没有二进制和十进制之分,数字就是数字。只有字符串才是某种进制的表达。不是字符串当然就不需要也不能转换了。
    onlyice
    onlyice
    Sep 30, 2017
    15
    @ericbize 我感觉这种 level 的代码,抄不抄都无所谓的

    比如你要开个平方根,有一些快速开平方根的代码已经实现好了,你难道自己再写一个?

    另外,同样不理解问题是什么。。如果只是 Python 层面的代码,Google 一下 "python convert binary to decimal"
    XIVN1987
    XIVN1987
    Sep 30, 2017 via Android
    16
    感觉传感器发过来的就是字节串吧,,那一串 0 和 1 本来就是一个个 char,所以应该先构造字节串,然后用 int('001101', 2)转成数字,,至于怎么构造字节串:
    s=''
    每来一个字节 ch,执行
    s += chr(ch)
    最后
    i = int(s, 2)
    ericbize
    ericbize
    OP
    Sep 30, 2017
    17
    @ltux 传感器输出的就是高低电压(第四页) 连接看 #9
    ericbize
    ericbize
    OP
    Sep 30, 2017
    18
    @msg7086 emmmm 我自己都乱了,表达不好
    ericbize
    ericbize
    OP
    Sep 30, 2017
    19
    @onlyice 关键是有 turnitin 查重啊~~~
    ericbize
    ericbize
    OP
    Sep 30, 2017
    20
    @XIVN1987 #17 楼 , 其实输出的是高低电压
    XIVN1987
    XIVN1987
    Sep 30, 2017
    21
    @ericbize
    那也可以用我说的那种办法

    s = ''
    for i in range(LEN):
    if SENSOR_PIN_LEVEL == 1: s += '1'
    else: s += '0'
    i = int(s, 2)
    XIVN1987
    XIVN1987
    Sep 30, 2017
    22
    ·
    s = ''
    for i in range(LEN):
    if SENSOR_PIN_LEVEL == 1: s += '1'
    else: s += '0'
    i = int(s, 2)
    ·
    XIVN1987
    XIVN1987
    Sep 30, 2017
    23
    ··· python
    s = ''
    for i in range(LEN):
    if SENSOR_PIN_LEVEL == 1: s += '1'
    else: s += '0'
    i = int(s, 2)
    ···
    XIVN1987
    XIVN1987
    Sep 30, 2017
    24
    v2ex 怎么贴代码才能保留空格缩进啊???
    msg7086
    msg7086
    Sep 30, 2017
    25
    @XIVN1987 gist。

    @ericbize 如果输出的是二进制的话,我猜你需要用 unpack 家族。
    xierch
    xierch
    Sep 30, 2017
    26
    你要直接判断高低电压,一个 bit 一个 bit 地读么…
    那你就读到 1 的时候,加一然后向左移一位( n += 1, n << 1 );
    读到 0 的时候,直接向左移一位( n << 1 )…
    ericbize
    ericbize
    OP
    Oct 1, 2017
    27
    @xierch 对啊,一 bit 一 bit 得读出来,然后写入 array 里面,最后计算。
    ericbize
    ericbize
    OP
    Oct 1, 2017
    28
    @XIVN1987 你的头像想表达什么 ?
    ericbize
    ericbize
    OP
    Oct 1, 2017
    29
    @msg7086 #13 楼的就可以了~~~
    XIVN1987
    XIVN1987
    Oct 1, 2017 via Android
    30
    @ericbize
    这不就是 ipython 的图标吗,,表示喜欢 ipython
    xierch
    xierch
    Oct 2, 2017
    31
    @ericbize 所以你直接用一个 int 来存就好了嘛… 边读变转这样…
    #13 的还是先转 str 的嘛(
    ericbize
    ericbize
    OP
    Oct 2, 2017
    32
    @xierch 对啊 13 楼的还是先转 str 但是不知道为什么能得到正确结果哎😓 。

    其实寿面还要校验 , 而且我不知道 >> << | 这些怎么用 。 还没学会 ╮(╯▽╰)╭
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1881 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 42ms · UTC 15:44 · PVG 23:44 · LAX 07:44 · JFK 10:44
    ♥ Do have faith in what you're doing.