Domanda

devo convertire stringhe Unicode in Python ad altri tipi come unsigned e firmato int 8 bit, unsigned e firmato int 16 bit, unsigned e firmato int 32 bit, unsigned e firmato int 64 bit, doppio, galleggiante, stringa, unsigned e firmato 8 bit, unsigned e firmato a 16 bit, senza segno e con segno a 32 bit, senza segno e con segno a 64 bit.

ho bisogno di aiuto da u persone.

È stato utile?

Soluzione

int() per convertire la stringa in un intero. Python non ha diversi numeri interi a larghezza fissa in modo devi semplicemente ottenere un tipo di cosa fuori.

Quindi utilizzare struct imballare il numero intero in una larghezza fissa:

res = struct.pack("=B",i) ## uint8_t
res = struct.pack("=b",i) ## int8_t

res = struct.pack("=H",i) ## uint16_t
res = struct.pack("=h",i) ## int16_t

res = struct.pack("=I",i) ## uint32_t
res = struct.pack("=i",i) ## int32_t

res = struct.pack("=Q",i) ## uint64_t
res = struct.pack("=q",i) ## int64_t

res = struct.pack("=f",i) ## float
res = struct.pack("=d",i) ## double

struct produce un byte-stringa contenente il numero in binario.

EDIT: Dai commenti che suona come si desidera solo per convertire la stringa (di cifre decimali) in un numero intero. Basta usare int() per questo, ma non sarà possibile ottenere tutte le trabocco complicata / semantica underflow dei tipi specificati. Non si può riprodurre quel in python, almeno non senza scrivere un sacco di codice.

Penso che se volete qualsiasi altro aiuto dovrete essere più precisi su ciò che si vuole raggiungere.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top