Im building a server in python,ineedtoconvertadecimalvaluetohexlikethis :
let ' ssaythepacketstartby4byteswhichdefinethepacketlenght 3360
00 00 00 00
ifthe len (包)=255 we would send :
00 00 00 ff
nowmyproblemisthatsometimesthepacketisbiggerthan 256 as for example 336,then it would be :
00 00 01 50
i dont know how to do that in python,andiwillreallyappreciateanyhelp。
解决方案导入结构
struct.pack(I ),336 ) )。
'x00x00x01P '
thestructmodulepacksandunpackspythonvaluesintobytes.the ' I ' formatmeansbig-endian4- byte integer。