I am new to hexadecimals and couldn't find any similar previous posts. I have a hexadecimal string 50f116bcf
, b3b4d25d0
, for example, that is known to be a 36-bit integer. Not sure if it's signed or unsigned. I was wondering how I can go about converting the hexadecimal to 38-bit integer to two 18-bit integers in python.
I saw in this post about converting hex to 16-bit signed integer using
def twos_complement(hexstr,bits):
value = int(hexstr,16)
if value & (1 << (bits-1)):
value -= 1 << bits
return value
twos_complement('FFFE',16)
-2
Would this generalize to a 38-bit integer as well twos_complement('b3b4d25d0',38)
?
For splitting an integer, I saw this post splitting a 16-bit int (x
variable) to two 8-bit using
c = (x >> 8) & 0xff
f = x & 0xff
(1030333333 >> 8) & 0xff
163
(1030333333 >> 8)
4024739
Can this also be generalized to splitting a known 38-bit integer to two 16-bit integer using the method below?
c = (x >> 16) & 0xff
f = x & 0xff