ruffiano posted:
Hello,
I need your expertise to perform a conversion.
I need to convert a std::string to a char[32]. However, in the
conversion, I need to get rid of the '/0' character. The initial string
represents a 256 bit (32 byte) numeric value. Therefore, the resulting
char[32] should not contain '/0'.
There's no need to get rid of the null terminator; just pretend it isn't
there.
If you have any suggestions, please let me know.
#include <cassert>
#include <string>
#include <iostream>
using std::string;
using std::cout;
unsigned const magic = 32;
char const inline (&Func(string const &str))[magic]
{
char const *const p = str.c_str();
assert(strlen(p) >= magic);
return reinterpret_cast<char const (&)[magic]>(*p);
}
int main()
{
string str("abcdefghijklmnopqrstuvwxyzabcdef");
char const (&arr)[magic] = Func(str);
char const *p = arr;
char const *const p_over = arr + magic;
do
{
cout << *p++;
} while (p != p_over);
}
--
Frederick Gotham