Skip to content

Instantly share code, notes, and snippets.

@thequbit
Created March 20, 2016 00:53
Show Gist options
  • Save thequbit/1ddf4980ddce30ece9f9 to your computer and use it in GitHub Desktop.
Save thequbit/1ddf4980ddce30ece9f9 to your computer and use it in GitHub Desktop.
def u8s_to_i16(low_u8, high_u8):
# get the high byte without the top bit ( sign bit )
high_byte = high_u8 & 127
# get the sign bit as a 1 ( negative ) or a 0 ( possitive )
high_byte_sign = (high_u8 & 128) >> 7 # get top bit
# get the low byte ( no adjustments needed )
low_byte = low_u8
# generate -1 for negative, or 1 for possitive
sign = -1 if high_byte_sign == 1 else 1
# shift the high byte up by 8 bits, add the low byte, and multiply by the sign bit
i16 = sign * ( (high_byte * 256) + low_byte )
return i16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment