C++ to Java String Conversion (using jace)

2005-11-10
2013-04-29
  • DSEnterprise

    DSEnterprise - 2005-11-10

    hi,

    I have a c++ application which calls a java function to which i need to pass a c++ string

    C++ string (std::string) is utf8 encoded japanese language string and i am using
    jace to convert my c++ string to java string

    I am using a english locale machine for running this application,
    the application works fine with japanese locale machine

    The java string I get in the java function called has 2 initial charactors currupted in it

    To see the exact problem i did the following

    I converted the std::string (utf8 encoded string) to JString(jace.proxy.java.lang.string)
    and then converted JString(jace.proxy.java.lang.string) to std::string

    This the piece of code

    dataVal is of type std::string

    ---
    JString jStr(dataVal);

    string testStr(jStr);

    cout << testStr
    ---

    Please suggest me any other way of doing this, or what is wrong with the above code

    The actual and resulting strings are as follows (On windows locale)

    Actual:: から検索user.small.29

    Result String:: ?ら検索user.small.29

     
    • Michael

      Michael - 2007-07-26

      I know this is very late, but thought I'd post here for the sake of others... spread the knowledge.

      You could construct the Java string using a JArray of JByte, and then tell Java to make the string by using the byte array using the UTF-8 character set.

      ----
      // Construct JArray of JBytes
      const int byteLength = myString.length();
      jace::JArray<jace::JByte> javaBytes(byteLength);

      // Copy the UTF-8 C-string characters into JArray
      while (int i = 0; i < byteLength; ++i)
      {
          javaBytes[i] = myString[i];
      }

      // Construct Java string with explicit UTF-8 character set
      jace::String javaString(javaBytes, "UTF-8");
      ----

      As a side note - the ICU4C libraries make portable C++ Unicode strings fairly easy.  It uses an internal UTF-16 data type, which can be used to 'go direct' to create a Java string.  If you want portability in your C++ code, then I'd strongly urge people to look at the ICU library: http://www.icu-project.org/

      If you're doing a Windows-only application, the wchar_t type is UTF-16 when compiling with Unicode enabled.  You can also construct Java strings using an array of JChar in the same way above.  I'd *definitely* stay away from wchar_t if you start mixing in UNIX platforms and switch to using the ICU libraries.

      -Michael

       

Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks