フィルターのクリア

Float to binary string and vice-versa

4 ビュー (過去 30 日間)
Marc
Marc 2013 年 5 月 21 日
Hello,
I would like to have following 2 functions written in Matlab (here the C-code):
void getMemoryFromFloat(float floatNumber,char memory[4])
{ // floatNumber is input, memory is output
memory[0]=((char*)&floatNumber)[0];
memory[1]=((char*)&floatNumber)[1];
memory[2]=((char*)&floatNumber)[2];
memory[3]=((char*)&floatNumber)[3];
}
float getFloatFromMemory(char memory[4])
{ // memory is input
return(((float*)memory)[0]);
}
Anyone can give me a hint? To give a quick background, I basically communicate with a C-library that sends me float data that is coded in a string. In a similar way, that library only accepts float data coded in a string.
Thanks!

採用された回答

Iain
Iain 2013 年 5 月 21 日
編集済み: Iain 2013 年 5 月 21 日
typecast will convert a 32bit float to a single 32bit integer, or into 4 8 bit integers, or 4 characters. (And vice versa).
32 bit float = single format

その他の回答 (1 件)

Marc
Marc 2013 年 5 月 21 日
Thanks a lot! typecast was the command I was looking for :)
  1 件のコメント
James Tursa
James Tursa 2013 年 5 月 21 日
If you are working with very large variables and/or doing this many times in a loop, you might consider this FEX submission "typecastx" that returns a shared data copy of the input. It is faster and more efficient than the built-in typecast function which returns a deep data copy.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by