0

First things first, I've looked around and found this:

In python, how to convert a hex ascii string to raw internal binary string?

https://docs.python.org/3/library/base64.html

Which is nice, but seems to involve converting hexadecimal to a binary representation. And base64 doesn't seem to support what I want, or am I being silly?

What I have is code from Matlab that outputs data in the form of 16 bit signed binary. But it should be saved as a raw binary, and not .m. So I've pushed it to a .txt file. And thus it lives its life as ASCII, which is nice and all but I need it as a pure .bin.

I've tried being lazy about this and searched for tools to simply let me copy and paste my data over to a dummyfile. But for whatever reason I've failed that.

So fine fine, I could write a python code that opens up the data and chunks the data into 16bit blocks some logic code and write it as it's ACII implementation, then do a (https://docs.python.org/2/library/binascii.html) conversion and write to the file?

This seems horribly cludgy (probably because it is). How can I get about this in a more elegant, or more preferably, lazy manner?

===

Short version: I have data like 0000111001000011 (signed 16) in ASCII encoding, I want that to be 0000111001000011 in raw bin. Me dumb, need help.

Community
  • 1
  • 1
  • Perhaps you could show some code you've written to solve the problem? – Erik Oct 26 '15 at 14:43
  • You need to be more precise with your terms. When you say "ASCII" do you mean a 16 character string? – Mark Ransom Oct 26 '15 at 15:15
  • Converting binary to ASCII: http://stackoverflow.com/questions/10411085/converting-integer-to-binary-in-python. Converting ASCII to binary: `int(s, 2)`. Converting unsigned 16 bit to signed: `x if x < 0x8000 else x-0x10000`. – Mark Ransom Oct 26 '15 at 15:28
  • If you want it in raw binary, why save it out as ascii in the first place? Matlab can write to binary - use `fwrite` and specify precision. – nkjt Oct 26 '15 at 16:13

3 Answers3

0

use int(string,base) with your input string and base 2 for binary data.

a = int('0000111001000011',2)

where a will be 3651

now you can write it to a file using the python struct

import struct   
outString = struct.pack('i',a) # i for signed int 16bit, 
                               # for others check the documentation

f = open('outfile','w')
f.write(outString)
f.close()
Rolf Lussi
  • 615
  • 5
  • 16
  • That is a very nice pointer, and one that is usefull (see code below). But it doesn't help push the binary data in its raw format to a .bin file. :) – user5489571 Oct 28 '15 at 12:02
  • edited the answer, so you see how you can write it as binary data to a file – Rolf Lussi Oct 28 '15 at 14:54
0

It seems you want to write 16-bit binary raw from Matlab?

Just use the 'fwrite' command in matlab, e.g.

fid=fopen('mybinaryfile.bin','wb')
fwrite(fid,data,'uint16')
fclose(fid)
Felix Darvas
  • 507
  • 3
  • 5
0

Thanks for the fwrite pointers. (OK, something that actually works this time) ed. nop, it doesn't do a raw binary write. It's some formatting going on...

Beware! Python and bytes likes to do formatting that will differ when using file.write(list_of_bytes). That caused me headaches trying to get around that before going back to the bitstring library and basically solving it in half a minute. sighs

The below code does what I want:

##Importing my needed tools
import bitstring

##Open the file with the data
data = open('Path\\data.txt');

##Read from the file
buffer = data.read();

##Convert the data to the proper format for transering
bin_list = bitstring.BitArray(bin = str(buffer));
b_array = bin_list.bin

##Create a c switch equivalent function of sorts, because of hex 
def cswitch(x):
    if ((x == '1') or (x == '2') or (x == '3') or (x == '4') or (x == '5') or (x == '6') or (x == '7') or (x == '8') or (x == '9') or (x == '0') or (x == 'a') or (x == 'b') or (x == 'c') or (x == 'd') or (x == 'e') or (x == 'f')):
    x = {'1':'01', '2':'02', '3':'03', '4':'04', '5':'05', '6':'06', '7':'07', '8':'08', '9':'09', '0':'00', 'a':'0a', 'b':'0b', 'c':'0c', 'd':'0d', 'e':'0e', 'f':'0f'}[x];
        return x;
    else:
        return x;

#Create a "hexifier" function
def hexifier(x):
    x = bytes.fromhex( cswitch((hex ( int( x[:8],2) ) )[2:]) )
    return x;

##Init shenanigans
b2_array = bitstring.BitArray(hexifier(b_array));
b_array = b_array[8:];

##Prepare to write to mem in byte chunks by building a hex array
for i in range (0, 2**13-1):    
    #Format the data in byte sized chunks
    b2_array.append(hexifier(b_array));
    b_array = b_array[8:];

## Write to a file, using the format .write() likes
with open('path\\binary_data.bin', 'br+') as f:
        f.write(b2_array.bytes);

##Close the files   
data.close();   
f.close();
  • As far as I know, fwrite with the 'b' option in Matlab does no formatting at all, but writes the raw bytes out, which can be easily confirmed, by e.g. loading the data back in with python. Not sure why you would want to run all that extra code? – Felix Darvas Nov 12 '15 at 23:14
  • The end reason is esoteric. I've got an embedded environment where I want to use a table. Due to legacy and what not, – user5489571 Nov 16 '15 at 11:28
  • (Because the b option didn't occur to me, is the real reason of course) – user5489571 Nov 16 '15 at 11:35