float array to binary string and vice-versa?
1 view (last 30 days)
Show older comments
Hello, In order to exchange information with an external application, I need to "pack" an array of numbers into a string, and vice-versa. How can I achieve this? I need to do this with 32bit floats and 32bit unsigned integers.
The external application sends the data like following:
float myFloatArray[4];
char* dataToSend=(char*)myFloatArray;
sendData(dataToSend,4*4); // 4*4 is the size of the data
Data is packed as little Endian.
Thanks for any insight!
Answers (2)
José-Luis
on 17 May 2013
Edited: José-Luis
on 17 May 2013
I don't exactly understand what you are trying to achieve in Matlab. The code you show is C/C++. Also, there are no pointers in Matlab (not explicitely at least). You could save your floats as a binary file, using little endian byte order. There is no need to typecast:
your_float = single(rand(1,4));
%Saving it as binary stream
fid = fopen('myBin.bin','w','l'); %little endian
fwrite(fid,your_float,'single');
fclose(fid);
The you could read that file in your C/C++ code. Or do you actually mean that you want a string of zeros and ones? That is a different thing.
0 Comments
See Also
Categories
Find more on Write C Functions Callable from MATLAB (MEX Files) in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!