I need to convert this array of bytes to string value? https://i.stack.imgur.com/lZMms.jpg
this array of bytes stored in memory like that: https://i.stack.imgur.com/1ciyu.jpg
the expected output -51.052277
I need to convert this array of bytes to string value? https://i.stack.imgur.com/lZMms.jpg
this array of bytes stored in memory like that: https://i.stack.imgur.com/1ciyu.jpg
the expected output -51.052277
I believe Etienne de Martel is correct: this looks like string of UTF-16LE code points, but without a 0 code point to terminate the string).
We can convert it to a floating point type something like this:
#include <iostream>
#include <vector>
#include <string>
#include <iomanip>
int main() {
std::vector<char> inputs { 45, 0, 53, 0, 49, 0, 46, 0, 48, 0, 53, 0, 50, 0, 50, 0, 55, 0, 55, 0 };
wchar_t const* data = reinterpret_cast<wchar_t const*>(inputs.data());
std::wstring cp(data, inputs.size()/2);
double d = std::stod(cp);
std::cout << std::setw(10) << std::setprecision(10) << d << '\n';
}
This depends on wchar_t
meaning UTF-16LE, which is the case with the compiler I'm using, but isn't required. In theory, you should probably use char16_t
and std::u16string
instead, but that doesn't work out well either (it'll create the string all right, there's no std::stod
for std::u16string
).