Split 2-byte integer into two single bytes

Python:

""" two byte integer """
val = 42131 # bin: 1010 0100 1001 0011 hex: 0xa493
print hex(val)
 
byte1 = val & 0xFF # first 8 bit
byte2 = (val >> 8) & 0xFF # last 8 bit
 
print "byte1: " + hex(byte1) # result: 0x93
print "byte2: " + hex(byte2) # result: 0xa4
 
""" merge two bytes into 2-byte integer """
val_m  = (byte1 & 0xFF)
val_m |= (byte2 & 0xFF) << 8
print hex(val_m) # result: 0xa493

Hinterlasse eine Antwort

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind markiert *

*

Du kannst folgende HTML-Tags benutzen: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">