Tell me more ×

What would be the algorithm/implementation of the C++ code C++functionX in the following flow chart:

(JavaString) --getBytes--> (bytes) --C++functionX--> (C++String)

JavaString contents should match C++String contents as far as possible (preferably 100% for all possible values of JavaString)

[EDIT] The endianness of bytes can be ignored as there are ways to handle that.

share|improve this question
How is the Java string going to C++? Via a file? –  Mooing Duck Sep 15 '11 at 22:59
You mean something like from one programming language to another ? –  Chuck Birkin Sep 15 '11 at 22:59
@Mooing Duck: The java bytes are encoded base64 and transferred over the wire using XMPP. –  Jus12 Sep 15 '11 at 23:26
add comment

4 Answers

up vote 3 down vote accepted


String original = new String("BANANAS");
byte[] utf8Bytes = original.getBytes("UTF8");
//save the length as a 32 bit integer, then utf8 Bytes to a file


int32_t tlength;
std::string utf8Bytes;
//load the tlength as a 32 bit integer, then the utf8 bytes from the file
//well, that's easy for UTF8

//to turn that into a utf-18 string in windows
int wlength = MultiByteToWideChar(CP_UTF8, 0, utf8Bytes.c_str(), utf8Bytes.size(), nullptr, 0);
std::wstring result(wlength, '\0');
MultiByteToWideChar(CP_UTF8, 0, utf8Bytes.c_str(), utf8Bytes.size(), &result[0], wlength);
//so that's not hard either

To do this in linux, one uses the iconv library, which is incredibly powerful, but more difficult to use. Here's a function that converts a std::string in UTF8 to a std::wstring in UTF32:

share|improve this answer
Yep :) that's how it should work :). –  Mihai Claudiu Toader Sep 15 '11 at 23:55
I have to object to that. mbtowc (I think you mean mbstowcs) has no notion of an explicit encoding. It's purpose is not to convert from UTF8. You really want to use a library that deals with explicit encodings, such as iconv(). The only purpose of the mb/wc functions in ANSI-C is to convert between char and wchar_t, in a platform-dependent fashion. –  Kerrek SB Sep 15 '11 at 23:58
@Kerrek: I origionally had MultiByteToWideChar, but then found when looking for something cross-platform –  Mooing Duck Sep 16 '11 at 0:00
mbstowcs doesn't do what you claim. It's purpose is a different one. It may work in practice, but not because it's guaranteed to. –  Kerrek SB Sep 16 '11 at 0:06
@Kerrek: I misread the docs. I switched to OS-dependant code, but I've never worked with linux before, so that's just a guess. –  Mooing Duck Sep 16 '11 at 0:10
show 3 more comments

There's no such thing as One True C++ String class. STL alone has std::string and std::wstring. That said, most string classes have a constructor that takes raw byte pointer as a parameter. The bytes come in the const char * form. So, a good example of your C++functionX is the constructor std::string::string(const char*, int).

Note the encoding issues. getBytes() takes an encoding as a parameter; you better match that on the C++ side, or you'll get jumble. If not sure, use UTF-8.

Depending on what kinds of Java strings you have, you might want to choose either regular or wide strings (e. g. std::wstring). The latter is a slightly better representation of what Java String offers.

share|improve this answer
According to, A String represents a string in the UTF-16 format, which is like std::wstring sometimes. std::wstring is 16 bytes on Windows, but not Linux. –  Mooing Duck Sep 15 '11 at 23:02
Depending on what the OP wants to do to his strings, and what's the nature of the content, a single-byte string in UTF-8 might do just as well. You'd be surprised how many text propcessing tasks are quite ASCII-friendly. –  Seva Alekseyev Sep 15 '11 at 23:04
I don't know about the STL, but the C++ standard library has 4 string classes (all specializations of std::basic_string): std::string, std::wstring, std::u16string, and std::u32string. In this case, I think std::u16string would fit very nicely :) –  R. Martinho Fernandes Sep 15 '11 at 23:10
@Mooing: 16 bytes? That's one wide character. –  Ben Voigt Sep 15 '11 at 23:14
depending on what he's doing he may run into endian issues with that, but it might be the best fit. –  Mooing Duck Sep 15 '11 at 23:19
show 2 more comments

C++, as far as the standard goes, doesn't know about encodings. Java does. So, to interface the two, make Java emit some well-defined encoding, such as UTF8:

byte[] utf8str = str.getBytes("UTF8");

In C++, use a library such as iconv() to transform the UTF8-string either into another string of a well-defined encoding (e.g. std::u32string with UTF-32, if you have C++11, or std::basic_string<uint32_t> or std::vector<uint32_t> otherwise), or, alternatively, convert it to WCHAR_T encoding, to be stored in a std::wstring, and proceed further to convert this to a multi-byte string via the standard function wcstombs() if you wish to interface with your environment.

The choice depends on what you need to do with the string. For serialization or text processing, go with the definite encoding (e.g. UTF-32). For writing to the standard output using the system's locale, use the multibyte conversion. (Here is a slightly longer discussion of encodings in C++.)

share|improve this answer
add comment

the C++ string should probably be an std::wstring instance and you would alse need to keep track of the encoding you would use to transform from JavaString to bytes.

This article will probably help you more:

std::wstring VS std::string

share|improve this answer
add comment

Your Answer


