繁体   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