3

I have to "translate" a:

char data[32]

into a:

tstring data;

Where the tstring is a typedef to std::string or a std::wstring depending on a preprocessor definition.

Assuming that data[32] contains only ASCII characters, what whould be the easiest way to pass this data into a tstring object?

Dejwi
  • 4,393
  • 12
  • 45
  • 74
  • Um if your preprocessor macros decide that `xstring` is `std::string` just initialise it with `data` and `32` ... otherwise, look up how to initialise a `std::wstring` from `char[N]` on your system? – Lightness Races in Orbit Aug 26 '15 at 15:03
  • 1
    Are you forced to use c-styled arrays? Is std::array a possibility? I ask because the solution is a tad more elegant. – Freddy Aug 26 '15 at 15:12
  • 1
    @dej Please avoid **bold** formatting except when it's *really* necessary (which is not very often at all). Here, code formatting is certainly a much better choice. – dandan78 Aug 26 '15 at 15:19

2 Answers2

7

Since you don't need to do any character conversions you could initialize both of the strings from with a vector of characters. Consider this example:

#include <string>
#include <vector>

int main()
{
    char data[32];
    std::vector<char> v(data, data + 32);
    std::string str(v.begin(), v.end());
    std::wstring wstr(v.begin(), v.end());
}
Rudolfs Bundulis
  • 11,636
  • 6
  • 33
  • 71
  • This was also proposed here http://stackoverflow.com/a/8969776/4181011 . This will fail for multi byte encodings. Edit: No criticism, just for information. – Simon Kraemer Aug 26 '15 at 15:26
  • @SimonKraemer yes it will, I posted this only because the OP stated that he has an ASCII string. If you think that the first sentence does not emphasize that strongly enough I can edit since I don't want somebody using this with UTF-8 and then blaming me:D – Rudolfs Bundulis Aug 26 '15 at 15:31
  • I think it's ok - even though I missed the first part reading it the first time. With all these comments this shouldn't be an issue anymore ;-) – Simon Kraemer Aug 26 '15 at 15:35
2

EDIT: The temporary std::string is not needed. I have updated my answer with an example.

See here: C++ Convert string (or char*) to wstring (or wchar_t*)

#include <locale>
#include <codecvt>
#include <string>

#define XWSTRING

#ifdef XWSTRING
typedef std::wstring xstring;
#else 
typedef std::string xstring;
#endif

xstring initString(const char* data)
{
#ifdef XWSTRING
    static std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
    return xstring(converter.from_bytes(data));
#else
    return xstring(data);
#endif
}


int main()
{
    char in_data[32] = "HELLO WORLD";
    xstring out_data = initString(in_data);
}
Community
  • 1
  • 1
Simon Kraemer
  • 5,700
  • 1
  • 19
  • 49
  • On sane systems, wchar_t is 32 bits wide and putting utf16 in wstring is weird to say the least. – Cubbi Aug 26 '15 at 15:25
  • Slightly overkill, given plain ASCII input? – Alan Stokes Aug 26 '15 at 15:25
  • 1
    @Cubbi Sane systems don't have `xstring` though. (It's a Microsoft specific thing on Windows.) – Alan Stokes Aug 26 '15 at 15:28
  • @AlanStokes Are you sure OP is refering to the MS version of `xstring`? "Where the xstring is a typedef to std::string or a std::wstring depending on a preprocessor definition." – Simon Kraemer Aug 26 '15 at 15:31
  • I've never come across such a thing on any other platform, but you're right that it's not certain from the question. – Alan Stokes Aug 26 '15 at 15:35
  • I assume most devs would name such a typedef type `tstring` and not `xstring` – Simon Kraemer Aug 26 '15 at 15:37
  • @AlanStokes I just found this: _" is a Microsoft C++ header containing the actual implementation of the std::basic_string template. You never need to include yourself. includes it for the basic_string implementation."_ – Simon Kraemer Aug 26 '15 at 15:39