Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7746995
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T10:25:15+00:00 2026-06-01T10:25:15+00:00

I’m using JNI to interface between a Java program and a C++ function. The

  • 0

I’m using JNI to interface between a Java program and a C++ function. The C++ function deals with multi-byte strings (CP 1252). I use this C++ code to convert the Java String to a char*:

char *arg=(char*) jEnv->GetStringUTFChars(jArg2,0);

This works fine unless I have some high-order characters. For example, if my input is:

Àlan (UTF: c2 6c 61 6e 20 4a 6f 6e 65 7e)

I can see that the resultant arg is:

c3 82 6c 61 6e

But, I would expect to see:

c0 6c 61 6e

Seeing that GetStringUTFChars() is supposed to return UTF strings, I tried obtaining the Unicode string with GetStringChars() and converting it via WideCharToMultiByte():

const jchar *str=jEnv->GetStringChars(jArg2,0);
WideCharToMultiByte(CP_UTF8,0,(LPCWSTR) str,jEnv->GetStringLength(jArg2),str,szStr,0,0);

(you can assume that I’ve allocated str and set szStr properly). In this situation, I see this in the resultant str:

c3 82 6c 61 6e

I’ve tried other CP_ values for the first parameter to WideCharToMultiByte, none yield useful results (they either return the above or substitute a ‘?’ for the ‘À’.

I would expect that somehow I could get this resultant str:

c0 6c 61 6e

But so far, I’ve had no luck.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T10:25:16+00:00Added an answer on June 1, 2026 at 10:25 am

    Java uses a modified version of UTF-8. Here is a quote from Java’s documentation:

    Modified UTF-8 is not new to the Java platform, but it’s something
    that application developers need to be more aware of when converting
    text that might contain supplementary characters to and from UTF-8.
    The main thing to remember is that some J2SE interfaces use an
    encoding that’s similar to UTF-8 but incompatible with it. This
    encoding has in the past sometimes been called “Java modified UTF-8”
    or (incorrectly) just “UTF-8”. For J2SE 5.0, the documentation is
    being updated to uniformly call it “modified UTF-8.”

    The incompatibility between modified UTF-8 and standard UTF-8 stems
    from two differences. First, modified UTF-8 represents the character
    U+0000 as the two-byte sequence 0xC0 0x80, whereas standard UTF-8 uses
    the single byte value 0x0. Second, modified UTF-8 represents
    supplementary characters by separately encoding the two surrogate code
    units of their UTF-16 representation. Each of the surrogate code units
    is represented by three bytes, for a total of six bytes. Standard
    UTF-8, on the other hand, uses a single four byte sequence for the
    complete character.

    Modified UTF-8 is used by the Java Virtual Machine and the interfaces
    attached to it (such as the Java Native Interface, the various tool
    interfaces, or Java class files), in the java.io.DataInput and
    DataOutput interfaces and classes implementing or using them, and for
    serialization. The Java Native Interface provides routines that
    convert to and from modified UTF-8. Standard UTF-8, on the other hand,
    is supported by the String class, by the java.io.InputStreamReader and
    OutputStreamWriter classes, the java.nio.charset facilities, and many
    APIs layered on top of them.

    Since modified UTF-8 is incompatible with standard UTF-8, it is
    critical not to use one where the other is needed. Modified UTF-8 can
    only be used with the Java interfaces described above. In all other
    cases, in particular for data streams that may come from or may be
    interpreted by software that’s not based on the Java platform,
    standard UTF-8 must be used. The Java Native Interface routines that
    convert to and from modified UTF-8 cannot be used when standard UTF-8
    is required.

    The byte sequence c2 6c 61 6e 20 4a 6f 6e 65 7e is not valid under standard UTF-8. In cp1252, that same byte sequence would be the string Âlan Jone~ (notice  instead of À).

    Under standard UTF-8, the string Àlan Jone~ would be the byte sequence c3 80 6c 61 6e 20 4a 6f 6e 65 7e (notice c3 80 6c instead of c2 6c).

    All Java strings are natively UTF-16, so you don’t need to retreive the string as UTF-8. Use GetStringChars() to get a original UTF-16 encoded characters and pass them as-is to WideCharToMultiByte() specifying 1252 as the codepage (note, in your example you are using str for both the UTF-16 input buffer and the cp1252 output buffer – don’t get your variables confused!), eg:

    const jchar *str = jEnv->GetStringChars(jArg2,0); 
    char *cp1252 = NULL;
    int len = WideCharToMultiByte(1252, 0, (LPCWSTR)str, jEnv->GetStringLength(jArg2), NULL, 0, 0, 0);
    if (len > 0)
    {
        cp1252 = new char[len + 1];
        WideCharToMultiByte(1252, 0, (LPCWSTR)str, jEnv->GetStringLength(jArg2), cp1252, len, 0, 0); 
        cp1252[len] = 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I have thousands of HTML files to process using Groovy/Java and I need to
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I am reading a book about Javascript and jQuery and using one of the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.