简体   繁体   中英

C++ - Convert std::string of float array

This question is a follow up to C++ - Convert array of floats to std::string .

How to convert a std::string back to float array that was converted to string using reinterpret_cast .

Get the string's backing data pointer from the c_str() method. Then reinterpret_cast it back to the float pointer.

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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