-3

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

  • 2
    StackOverflow is not here to write your code for you. What have you tried? Do you know how to convert strings to floats in general in C++? What do you see in your current string that looks different than "usual"? Also, raw C strings (`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. – Silvio Mayolo Apr 14 '22 at 01:28
  • ofcourse, i did also checked these links [link](https://stackoverflow.com/questions/1673445/how-to-convert-unsigned-char-to-stdstring-in-c) and this one [link](https://stackoverflow.com/questions/57399227/convert-c-byte-array-to-a-c-string) and none of them working with me – Mathew Farrell Apr 14 '22 at 01:31
  • 1
    How would you want to convert this? The data is 20 bytes, but floating point numbers in cpp are 4 or 8 bytes. Or if you are using some extended definitions 10 or 16 bytes none of which are the 20 bytes you show? – Lala5th Apr 14 '22 at 01:31
  • 2
    This array really looks like it's actually a string encoded in little endian UTF-16 (aka UTF-16LE). – Etienne de Martel Apr 14 '22 at 01:31
  • What does the array of bytes represent exactly? Does it represent a string, which contains the digits of a number? Or is it the binary representation of a `float`? – Andreas Wenzel Apr 14 '22 at 01:32
  • @Lala5th the value is array of bytes stored in the memory – Mathew Farrell Apr 14 '22 at 01:33
  • 1
    @MathewFarrell I realise now, but the question is weirdly worded. What you want isn't to convert an array of bytes to float (essentially casting with extra steps), but to convert `char16_t` to `float`. – Lala5th Apr 14 '22 at 01:39
  • Does this answer your question? [Convert u16string to float](https://stackoverflow.com/questions/49232505/convert-u16string-to-float) – Lala5th Apr 14 '22 at 01:39
  • Try this. https://en.cppreference.com/w/cpp/string/basic_string/stof – Taekahn Apr 14 '22 at 01:40
  • @Taekahn I don't think this woukd work unless the system happens to have `wchar_t` as an alias to `char16_t` – Lala5th Apr 14 '22 at 01:46
  • @Lala5th nope .. – Mathew Farrell Apr 14 '22 at 01:54
  • @Lala5th aren’t they the same on windows? – Taekahn Apr 14 '22 at 01:57
  • You say you want to convert this array to a `float`, but then you give an expected output that is a string. Which do you want? `float` or string? There is no `float` value that corresponds exactly to that decimal string – Chris Dodd Apr 14 '22 at 02:00
  • 1
    @Taekahn The question isn't whether it is the same on windows or not, but if someone were to find this some time from now this assumption cannot be made – Lala5th Apr 14 '22 at 02:03
  • As a final note you can use a one liner like `std::stof(std::wstring_convert,char16_t>().to_bytes((char16_t*)data))` as long as the data was initialised as `char16_t*` and just cast away. If it was initialised as char or `std::byte` then to avoid UB you'd need to do memcpy and similar before doing this. This will convert `char16_t*` to `char*` and then convert that to `float`. Would've added a longer explanation as an answer but question was closed before then – Lala5th Apr 14 '22 at 02:13
  • 1
    @ChrisDodd I already did it, converted to string then used `std::stof` to convert to float value, I don't know what's wrong with you guys here! – Mathew Farrell Apr 14 '22 at 02:14
  • @Lala5th generic solution are always preferable, but the person asking the question is quite clearly using windows. I don’t think taking that into account is a bad thing, personally. – Taekahn Apr 14 '22 at 02:15
  • @Lala5th yeah, that's what I did thanks man – Mathew Farrell Apr 14 '22 at 02:16
  • @Lala5th Unfortunately, things like `std::codecvt` were deprecated in C++17. – Etienne de Martel Apr 14 '22 at 03:53

1 Answers1

2

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

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111