簡體   English   中英

C ++-轉換float數組的std :: string

[英]C++ - Convert std::string of float array

這個問題是C ++的后續工作-將浮點數組轉換為std :: string

如何將std::string轉換回使用reinterpret_cast轉換為float的float數組。

c_str()方法獲取字符串的后備數據指針。 然后將其reinterpret_cast返回到float指針。

const float* array_of_floats = reinterpret_cast<const float*>(str.c_str());
int len = str.size() / sizeof(float);

通常,將二進制數據(例如浮點數數組)序列化為string可以的,但充其量是很奇怪的,更不建議這樣做。 最好使用std::vector<uint8_t>作為字節數組來保存浮動指針數據而不是字符串實例。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM