2013-03-29 34 views
1

我有一個python函數用於設置winamp中的音量,因爲它是音量控制參數,所以它似乎只接受單個字節。當我嘗試傳遞一個字符串類型的數字時,它將音量設置爲看起來很奇怪的值。我認爲這是因爲它不是傳遞一個字節,而是讀取它傳遞的整數的高端字節。python單字節函數

我的問題是,如何確保字符串類型數字在發送前轉換爲範圍(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)") 

本質上我需要將Arg1強制轉換爲單個字節。 (我認爲)

回答

0

您可以嘗試使用結構

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

沒關係,

實際上是從字符串鑄造用它來詮釋作品:

int(Arg1) 

完整代碼:

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)")