简体   繁体   English

python单字节函数

[英]python single byte function

I have a python function for setting the volume in winamp that appears to only accept a single byte as it's volume control argument. 我有一个用于在winamp中设置音量的python函数,该音量似乎仅接受一个字节作为音量控制参数。 When I try to pass it a string type number it sets the volume to what appears to be very odd values. 当我尝试将其传递给字符串类型数字时,它将音量设置为看起来很奇怪的值。 I assume this is because rather than being passed a byte it is reading the upper big-end bytes of the integer it gets passed. 我认为这是因为不是读取一个字节而是读取它所传递的整数的高位大字节。

My question is, how do I ensure the string type number is converted to just a single unsigned byte in the range (0 - 255) before being sent ? 我的问题是,如何确保在发送之前将字符串类型号转换为(0-255)范围内的单个无符号字节?

if "volume" in message.lower():
    if (len(wordList) > 4):
        xcomPrint("Usage : \\winamp volume (0 to 100)")
    elif (Arg1.isdigit()):
        winampInstance.setVolume(Arg1)
        xcomPrint("Volume set to " + str(Arg1))
    else:
        xcomPrint("Incorrect Arguments")
        xcomPrint("Usage : \\winamp volume (0 to 100)")

Essentially I need to cast Arg1 to a single byte. 本质上,我需要将Arg1强制转换为单个字节。 (I think) (我认为)

Never mind, 没关系,

actually casting it from string to int works using: 实际将其从字符串转换为int可以使用:

int(Arg1)

complete code: 完整的代码:

if "volume" in message.lower():
    if (len(wordList) > 4):
        xcomPrint("Usage : \\winamp volume (0 to 100)")
    elif (Arg1.isdigit()):
        winampInstance.setVolume(int(Arg1))
        xcomPrint("Volume set to " + str(Arg1))
    else:
        xcomPrint("Incorrect Arguments")
        xcomPrint("Usage : \\winamp volume (0 to 100)")

You can try using struct 您可以尝试使用struct

import struct
Arg1 = struct.pack('B', 255)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM