Trakhan
Trakhan

Reputation: 483

Convert ICU UnicodeString to platform dependent char * (or std::string)

In my application I use ICU UnicodeString to store my strings. Since I use some libraries incompatible with ICU, I need to convert UnicodeString to its platform dependent representation.

Basicly what I need to do is reverse process form creating new UnicodeString object - new UnicodeString("string encoded in system locale").

I found out this topic - so I know it can be done with use of stringstream.

So my answer is, can it be done in some other simpler way, without using stringstream to convert?

Upvotes: 1

Views: 6696

Answers (3)

CWTstackoverflow
CWTstackoverflow

Reputation: 93

i use

std::string converted;
us.toUTF8String(converted);

us is (ICU) UnicodeString

Upvotes: 6

Steven R. Loomis
Steven R. Loomis

Reputation: 4350

You could use UnicodeString::extract() with a codepage (or a converter). Actually passing NULL for the codepage will use what ICU detected as the default codepage.

Upvotes: 3

Billy ONeal
Billy ONeal

Reputation: 106530

You could use the functions in ucnv.h -- namely void ucnv_fromUnicode (UConverter *converter, char **target, const char *targetLimit, const UChar **source, const UChar *sourceLimit, int32_t *offsets, UBool flush, UErrorCode *err). It's not a nice C++ API like UnicodeString, but it will work.

I'd recommend just sticking with the operator<< you're already using if at all possible. It's the standard way to handle lexical conversions (i.e. string to/from integers) in C++ in any case.

Upvotes: 0

Related Questions