2011-09-11 14 views
46

Come posso rappresentare un array di byte (come in Java con byte []) in Python? Dovrò inviarlo via cavo con gevent.Byte Array in Python

byte key[] = {0x13, 0x00, 0x00, 0x00, 0x08, 0x00}; 

risposta

45

In Python 3, usiamo l'oggetto bytes, noto anche come str in Python 2.

# Python 3 
key = bytes([0x13, 0x00, 0x00, 0x00, 0x08, 0x00]) 

# Python 2 
key = ''.join(chr(x) for x in [0x13, 0x00, 0x00, 0x00, 0x08, 0x00]) 

mi trovano più conveniente usare il modulo base64 ...

# Python 3 
key = base64.b16decode(b'130000000800') 

# Python 2 
key = base64.b16decode('130000000800') 

È inoltre possibile utilizzare letterali ...

# Python 3 
key = b'\x13\0\0\0\x08\0' 

# Python 2 
key = '\x13\0\0\0\x08\0' 
+4

Per la cronaca, invece di 'Base64. b16decode (x) ', puoi usare, semplicemente,' x.decode ("hex") '. Ti consente di cavartela con una importazione in meno, almeno. :) – Dolda2000

2

La risposta di Dietrich è probabilmente la cosa che ti serve per ciò che descrivi, inviando byte, ma un analogo più vicino al codice che hai fornito, ad esempio, userebbe il tipo bytearray.

>>> key = bytearray([0x13, 0x00, 0x00, 0x00, 0x08, 0x00]) 
>>> bytes(key) 
b'\x13\x00\x00\x00\x08\x00' 
>>> 
+0

Fine per Python 2.5 o precedenti, ma il 'bytearray' incorporato è davvero la strada da percorrere se si desidera, ehm, un array di byte. –

+0

@TokenMacGuy: La tua risposta richiede altre 2 modifiche: (1) menziona il modulo array (2) 'bytearray ('b', ...)' non funziona. O potresti semplicemente cancellarlo. –

+1

@ John: grazie, risolto. In futuro, vai avanti e apporta le modifiche da solo. – SingleNegationElimination

24

Basta usare un bytearray (Python 2.6 e successivi), che rappresenta una sequenza mutabile di byte

>>> key = bytearray([0x13, 0x00, 0x00, 0x00, 0x08, 0x00]) 
>>> key 
bytearray(b'\x13\x00\x00\x00\x08\x00') 

indicizzazione ottenere e imposta il byte individuo

>>> key[0] 
19 
>>> key[1]=0xff 
>>> key 
bytearray(b'\x13\xff\x00\x00\x08\x00') 

e se ne avete bisogno come str (o bytes in Python 3), è semplice come

>>> bytes(key) 
'\x13\xff\x00\x00\x08\x00' 
+0

Non così semplice con 3.x; 'fubar = str (chiave); print (len (tasto), len (fubar)) 'produce' 6 38'. In ogni caso (1) "string" è una terminologia molto vaga (2) se vuole la mutabilità, può mutare la sua lista ** originale ** –

+0

@John: buon punto su 'str' lavorare in modo diverso per' bytearray' in Python 3 - risolto. Ho menzionato la mutabilità principalmente per distinguerlo da 'bytes', ma il punto è anche che non è necessario avere un passaggio intermedio per avere i tuoi dati in un' elenco '. –

+2

È una buona probabilità che ciò di cui l'OP ha veramente bisogno sia qualcosa come struct.pack ("

5

Un'alternativa che ha anche il vantaggio di facilità di registrazione il suo output:

hexs = "13 00 00 00 08 00" 
logging.debug(hexs) 
key = bytearray.fromhex(hexs) 

permette di fare semplici sostituzioni in questo modo:

hexs = "13 00 00 00 08 {:02X}".format(someByte) 
logging.debug(hexs) 
key = bytearray.fromhex(hexs)