【问题标题】:what is happening to this python char array这个 python char 数组发生了什么
【发布时间】:2016-10-16 01:51:53
【问题描述】:
str = "hello this is a string"
buffer = map(ord,str) #string to char array
b = (buffer[0]) #i assume this is creating an array of one buffer[0]
fin = b & 0x80 #what does this do?

我不确定b & 0x80 在做什么,我假设它正在附加b 字节数组?

【问题讨论】:

    标签: python-2.7 operators


    【解决方案1】:

    大部分代码在做什么是对的:

    str = "hello this is a string"
    buffer = map(ord,str)
    print buffer
    

    缓冲区是一个列表,其中str 中的每个字符都被更改为它的 ascii 值(即使是空格也会被转换,它们的值是 32):

    [104, 101, 108, 108, 111, 32, 116, 104, 105, 115, 32, 105, 115, 32, 97, 32, 115, 116, 114, 105, 110, 103]
    

    下一行只是将b 设置为该列表的第一个索引,因此b 只是一个int,而不是一个int 列表。括号无关紧要:

    b = (buffer[0])
    bb = buffer[0]
    print b,bb #prints: 104 104
    

    & 是一个按位“与”运算符,它的工作原理是获取两个数字并返回一个新数字,其中位仅用于重叠位。 104的二进制是:

    print("{0:b}".format(104))
    '01101000'
    

    0x80 是十六进制的128,它具有二进制字符串'10000000'。所以 104 和 128 的 & 是两个数字的列,用二进制写成,有一个 1,恰好没有,所以结果为零:

    '01101000'
    '10000000'
     --------
    '00000000'
    

    这是128129 之间的示例,其结果为128

    '10000000'
    '10000001'
     --------
    '10000000'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-12
      • 2014-01-25
      • 2023-04-10
      • 1970-01-01
      相关资源
      最近更新 更多