Reputation: 570
I'm trying to put euro symbol in a Java string that is passed to a native function(using JNA) in this way:
/*JAVA*/
String s= new String("Euro symbol=€");
nativefunction(s.getBytes(US-ASCII));
/*C++*/
void nativefunction(char *s)
{
printf("%s",s);
}
native function output: Euro symbol=?
Why the symbol is printed as ?
instead €
.
I also tried to use ascii code of euro symbol(\0x80
) but the result is the same.
Upvotes: 3
Views: 2734
Reputation: 12817
Internally, Java encodes strings in UTF-16, which uses two bytes for each character. The UTF codepoint for the EURO SIGN is U+20AC, which is 0x20AC in the UTF-16 encoding. US-ASCII uses one byte for each character. Since the euro sign cannot be represented in US ascii, the encoder replaces this character with a question mark. Read about in in the CharSetEncoder documentation.
Upvotes: 0
Reputation: 242726
US-ASCII doesn't contain euro character. Perhaps you meant Windows-1252, if so, use:
nativefunction(s.getBytes("Windows-1252"));
If it still doesn't work, try to use Unicode escape sequence in Java code:
String s= new String("Euro symbol=\u20ac");
If it works for \u20ac
but doesn't work for €
, you need to configure source code encoding.
Upvotes: 4