float array to binary string and vice-versa?
1 次查看(过去 30 天)
显示 更早的评论
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!
回答(2 个)
José-Luis
2013-5-17
编辑:José-Luis
2013-5-17
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 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!