I have a binary np array below:
arr = [0. 0. 1. ... 0. 1. 1.]
I would like to convert them to the binary sequences stored as its ascii value of each element to a .bin file (the output file name is 'test.bin'). For example, zero's ascii value is 48 and its corresponding binary form is: 00110000. If there is a zero in the np array, there should be a corresponding 00110000 in the bin file I tried to use the code below:
buffer_string = ''.join(str(x) for x in arr.astype(int))
with open("test.bin", "wb") as f:
for x in buffer_string:
f.write(bytes(ord(x)))
But the outputs are all zeros. Could you please give me some suggestions?
Thanks
f.write(buffer_string)?'w', or explicitly encode the string as ASCII. I don't think it makes a difference in this case (since'0'and'1'have the same encoding in ASCII and UTF-8 afaik).