I need to convert this array of bytes to string value? https://i.sstatic.net/lZMms.jpg
this array of bytes stored in memory like that: https://i.sstatic.net/1ciyu.jpg
the expected output -51.052277
I need to convert this array of bytes to string value? https://i.sstatic.net/lZMms.jpg
this array of bytes stored in memory like that: https://i.sstatic.net/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).
char*) are assumed to be null terminated in a lot of places, so I hope you're keeping track of the size of that array in some other way, because all of the standard C-style functions are going to choke on it since you're using nulls in the middle.char16_ttofloat.