I had the same problem and I found a simpler and shorter solution.
Here's an example:
Encode:
In [1]: val = 15
In [2]: bin_ = '{0:016b}'.format(val)
In [3]: bin_
Out[3]: '0000000000001111'
Or:
In [4]: bin_ = bin(val)[2:].zfill(16)
In [5]: bin_
Out[5]: '0000000000001111'
Decode:
In [6]: int(bin_, 2)
Out[6]: 15
Answer from Benyamin Jafari on Stack OverflowI had the same problem and I found a simpler and shorter solution.
Here's an example:
Encode:
In [1]: val = 15
In [2]: bin_ = '{0:016b}'.format(val)
In [3]: bin_
Out[3]: '0000000000001111'
Or:
In [4]: bin_ = bin(val)[2:].zfill(16)
In [5]: bin_
Out[5]: '0000000000001111'
Decode:
In [6]: int(bin_, 2)
Out[6]: 15
This isn't a very popular question to answer, I can see, so I'll put this up in the meantime:
def convert_to_binary(value):
'''
Converts a float to a 16-bit binary string.
'''
n = ['0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0']
value = int(value)
if value > 2**15:
if value > 2**16:
print "Value too large"
else:
n[0] = '1'
value = value - (2**15)
if value > 2**14:
n[1] = '1'
value = value - (2**14)
if value > 2**13:
n[2] = '1'
value = value - (2**13)
if value > 2**12:
n[3] = '1'
value = value - (2**12)
if value > 2**11:
n[4] = '1'
value = value - (2**11)
if value > 2**10:
n[5] = '1'
value = value - (2**10)
if value > 2**9:
n[6] = '1'
value = value - (2**9)
if value > 2**8:
n[7] = '1'
value = value - (2**8)
if value > 2**7:
n[8] = '1'
value = value - (2**7)
if value > 2**6:
n[9] = '1'
value = value - (2**6)
if value > 2**5:
n[10] = '1'
value = value - (2**5)
if value > 2**4:
n[11] = '1'
value = value - (2**4)
if value > 2**3:
n[12] = '1'
value = value - (2**3)
if value > 2**2:
n[13] = '1'
value = value - (2**2)
if value > 2**1:
n[14] = '1'
value = value - (2**1)
if value >= 2**0:
n[15] = '1'
value = value - (2**0)
n = ''.join(n)
n = str(n)
print str(n)
return str(n)
def convert_back(bin_num):
"""
Converts binary string to a float.
"""
value = 0
print type(bin_num)
n = list(bin_num)
if n[0] == '1':
value = value + (2 ** 15)
if n[1] == '1':
value = value + (2 ** 14)
if n[2] == '1':
value = value + (2 ** 13)
if n[3] == '1':
value = value + (2 ** 12)
if n[4] == '1':
value = value + (2 ** 11)
if n[5] == '1':
value = value + (2 ** 10)
if n[6] == '1':
value = value + (2 ** 9)
if n[7] == '1':
value = value + (2 ** 8)
if n[8] == '1':
value = value + (2 ** 7)
if n[9] == '1':
value = value + (2 ** 6)
if n[10] == '1':
value = value + (2 ** 5)
if n[11] == '1':
value = value + (2 ** 4)
if n[12] == '1':
value = value + (2 ** 3)
if n[13] == '1':
value = value + (2 ** 2)
if n[14] == '1':
value = value + (2 ** 1)
if n[15] == '1':
value = value + (2 ** 0)
print value
return value
I'm trying to take a number (such as -200) and send it to a robot. The number must be converted to big-endian and sent in two bytes. WolframAlpha does this (the result I need to send is 38ff) but I haven't found a way to do this in python.
Check out hex() and, more importantly struct:
http://docs.python.org/2/library/struct.html
This lets you convert a decimal into binary data. I've used it to read binary files, so it might be what you are looking for. I'm weak on theory so I tend to just bang on stuff until I get it right, but I would start with:
>>> import struct
>>> struct.pack('>h', -200)
b'\xff8'
And see what the machine makes of it, and adjust accordingly.
u/1328 was close with struct, the code you are looking for is:
import struct
print struct.pack('>i', your_int)
>I is the indicator for Big Endian as according to struct.
Python Struct Documentation
EDIT: I just tested this code, and got the same result as u/1328. I'm looking into this more.
EDIT 2: OK. Wolfram's representation of the number -200 in hexadecimal is as an unsigned 16 bit integer. THey make a note that this number overflowed, and the reason is that -200 is, inherently, a signed number.
Are you sure you need to send the number is ff38, and not xff8?
If so, then you need to do some hackery to get this working. Something like:
import struct
def toBigEndian(number):
if number < 0:
number = 65536 + number
return struct.pack('>i',number)
I'm lost. I am trying to find away to convert a decimal to hex as a 16 bit interger using python.
I try this...
print (struct.pack('>i', 30))
Output is ...
b'\x00\x00\x00\x1e'
I was expecting a value of 042A for decimal 90 hex should be 0C80