def convert_output (output):
return (struct.pack('i', output))
while True:
pygame.event.pump()
joystick = pygame.joystick.Joystick(0)
joystick.init()
X_AX = int(joystick.get_axis(0) * 100)
DATA = convert_output(X_AX)
print("message:", (DATA), "----", hex(X_AX), "----", X_AX)
Comment obtenir la même sortie dans DATA que hexadécimal (X_AX)? J'ai besoin de X_AX comme octet HEX pour la communication UDP.Python3 Convertir un INT en HEX
Sortie courant ressemble:
message: b'\x00\x00\x00\x00' ---- 0x0 ---- 0
message: b'\x18\x00\x00\x00' ---- 0x18 ---- 24
gamme de X_AX est -100 à 100
merci! sock.sendto ((DATA), (HOST, PORT)) enverra l'octet brut? – user3603948
@ user3603948: Eh bien, vous pourriez omettre les parenthèses autour de 'DATA', mais oui,' sock.sendto (DATA, (hôte, port)) 'serait l'approche normale pour IPv4 (et IPv6 le supporte, bien que [le' socket' docs indique que vous devriez fournir 'flowinfo', et' scopeid' pour les adresses IPv6] (https://docs.python.org/3/library/socket.html#socket-families)). – ShadowRanger