Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ - Convert array of floats to std::string

I have an array of floats with a fixed length. Now I want to convert that array to a binary string.

I cannot use const char * because my string will contain null-bytes. How would I use memcpy in that case? I have already tried a reinterpret_cast<string *>, but that won't work because the string is also/only storing pointers to the begin and end of the data (correct me if I am wrong).

I'm already constructing an empty string:

string s;
s.resize(arr_size);

But how would I copy an array of floats to that string?

Basically, I want to dump the memory region of a fixed float array to a string.

Don't be to hard with me, I'm still learning c++

like image 949
tobspr Avatar asked Jul 31 '13 08:07

tobspr


People also ask

How to convert from float to string in c++?

We can convert float and double to string using the C++11 std::to_string() function. For the older C++ compilers, we can use std::stringstream objects.

What is ftoa in C?

Write a C function ftoa() that converts a given floating-point number or a double to a string. Use of standard library functions for direct conversion is not allowed. The following is prototype of ftoa(). The article provides insight of conversion of C double to string.

How to turn string into double C++?

std::stold : It convert string into long double. Syntax: long double stold( const string& str, size_t *pos = 0 ); long double stold (const wstring& str, size_t* pos = 0); Parameters : str : the string to convert pos : address of integer to store the index of the first unconverted character.

How do I convert an int to a string in C++?

The next method in this list to convert int to string in C++ is by using the to_string() function. This function is used to convert not only the integer but numerical values of any data type into a string. The to_string() method is included in the header file of the class string, i.e., <string> or <cstring>.


3 Answers

Like this:

#include <algorithm>
#include <string>

float data[10]; // populate

std::string s(sizeof data);
char const * p = reinterpret_cast<char const *>(data);

std::copy(p, p + sizeof data, &s[0]);

Note that sizeof data is the same as 10 * sizeof(float), i.e. the number of bytes in the array.

Update: As suggested by James, you can do even better and write it all in one go:

char const * p = reinterpret_cast<char const *>(data);
std::string s(p, p + sizeof data);  // beginning + length constructor

Or even:

#include <iterator>

std::string s(reinterpret_cast<char const *>(std::begin(data)),  // begin + end
              reinterpret_cast<char const *>(std::end(data)));   // constructor
like image 126
Kerrek SB Avatar answered Oct 03 '22 01:10

Kerrek SB


Getting all of the bytes of the array into a string is easy:

std::string
bitwiseDump( float const* begin, float const* end )
{
    return std::string( reinterpret_cast<char const*>( begin ),
                        reinterpret_cast<char const*>( end ) );
}

But why? There's nothing you can do with the string except copy it back into an array of the same type. (And even for that use, std::vector<char> or std::vector<unsigned char> would be more natural. And less obfuscating.)

like image 28
James Kanze Avatar answered Oct 03 '22 01:10

James Kanze


Take a look at this...

#include <iostream>
#include <array>
#include <string>

int main()
{
    std::string floatString;
    std::array<float, 5> data = {1.1f, 2.2f, 3.3f, 4.4f, 5.5f};

    for (auto& element : data)
        floatString.append(std::to_string(element));

    std::cout << floatString;
    std::cin.get();
}
like image 39
Unknown Soldier Avatar answered Oct 03 '22 01:10

Unknown Soldier