简体   繁体   English

C ++-转换float数组的std :: string

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

This question is a follow up to C++ - Convert array of floats to std::string . 这个问题是C ++的后续工作-将浮点数组转换为std :: string

How to convert a std::string back to float array that was converted to string using reinterpret_cast . 如何将std::string转换回使用reinterpret_cast转换为float的float数组。

Get the string's backing data pointer from the c_str() method. c_str()方法获取字符串的后备数据指针。 Then reinterpret_cast it back to the float pointer. 然后将其reinterpret_cast返回到float指针。

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

In general, serializing binary data (such as array of floats) into string can work, but is at best weird and more likely ill-advised. 通常,将二进制数据(例如浮点数数组)序列化为string可以的,但充其量是很奇怪的,更不建议这样做。 You are better off using std::vector<uint8_t> as an array of bytes to hold your floating pointer data instead of an instance of a string. 最好使用std::vector<uint8_t>作为字节数组来保存浮动指针数据而不是字符串实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM