Last active
August 29, 2015 14:01
-
-
Save Yepoleb/5538f16dd35e13ede0fa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
bincont = b"\x34\x83\x12\x6F" | |
def bitlist(inbytes): | |
output = [] | |
for byte in reversed(inbytes): | |
for i in range(0, 8): | |
bit = byte & (1 << i) | |
if bit: | |
output.append(1) | |
else: | |
output.append(0) | |
return output | |
def addbits(bits): | |
out = 0 | |
for bit in reversed(bits): | |
out = (out << 1) | bit | |
return out | |
def fractional_add(bits): | |
accum = 0 | |
for i, bit in enumerate(reversed(bits)): | |
accum += bit*(1/(2**(i+1))) | |
return accum | |
def decfloat(floatbytes): | |
bits = bitlist(floatbytes) | |
sign = bits[31] | |
expb = bits[23:31] | |
fractb = bits[0:23] | |
exp = addbits(expb) | |
fract = fractional_add(fractb) | |
print(fractb, fract) | |
return (-1)**sign * 2**(exp-127) * (1 + fract) | |
print(decfloat(bincont)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment