整数到字节转换

qua*_*ano 3 python byte casting

假设我有一个整数,13941412,我希望将其分成字节(数字实际上是0x00bbggrr形式的颜色).你会怎么做?在c中,您将数字转换为BYTE然后移位.你如何在Python中转换为byte?

msw*_*msw 13

使用按位数学运算符,"字节"已经存在:

def int_to_rgb(n):
    b = (n & 0xff0000) >> 16
    g = (n & 0x00ff00) >> 8
    r = (n & 0x0000ff)
    return (r, g, b)
Run Code Online (Sandbox Code Playgroud)