当前位置: 技术问答>java相关
JNI中文问题
来源: 互联网 发布时间:2015-03-29
本文导语: 各位大侠: 在JNI 中将中文字符传给VC,VC不能正确显示(printf). VC将中文字符返回给JAVA时, 在JAVA中也无法显示. import java.io.*; import java.util.*; class JniTest { public static native String webcall13(String...
各位大侠:
在JNI 中将中文字符传给VC,VC不能正确显示(printf).
VC将中文字符返回给JAVA时, 在JAVA中也无法显示.
import java.io.*;
import java.util.*;
class JniTest
{
public static native String webcall13(String prompt);
static {
System.loadLibrary("siuweb");
}
public static String GBToUnicode(String strIn)
{
String strOut = null;
if(strIn == null || (strIn.trim()).equals(""))return strIn;
try{
byte[] b = strIn.getBytes("ISO8859_1");
//byte[] b = strIn.getBytes("GB2312");
//byte[] b = strIn.getBytes("UTF8");
//byte[] b = strIn.getBytes("ISO-8859-1");
strOut = new String(b,"GBK");
//strOut = new String(b,"gb2312");
}
catch(Exception e){}
return strOut;
}
public static String UnicodeToGB(String strIn)
{
byte[] b;
String strOut = null;
if(strIn == null || (strIn.trim()).equals(""))
return strIn;
try{
b = strIn.getBytes("GBK");
strOut = new String(b,"ISO8859_1");
}catch(UnsupportedEncodingException e){}
return strOut;
}
public static void main(String[] args)
{
JniTest test = new JniTest();
//System.getProperties().list(System.out);
test.webcall1();
int count = test.webcall2(2,5);
String str_tmp;
str_tmp = "JA你webcall3:";
String str_input;
str_input = UnicodeToGB(str_tmp);
String input = test.webcall13(str_input);
/* System.out.println("C return string : " + input); */
String str_output;
str_output = GBToUnicode(input);
System.out.println("C return Unicode string : " + str_output);
}
}
JNIEXPORT jstring JNICALL
Java_JniTest_webcall13 (JNIEnv *env, jclass obj, jstring a)
{
jstring jstr;
char greeting[100];
const char *str = (*env)->GetStringUTFChars(env,a,0);
wprintf(L"C wprintf java string: %sn", str);
printf("nC printf java string: %sn", str);
(*env)->ReleaseStringUTFChars(env, a, str);
greeting[0]='';
strcpy(greeting,"abk中cedasdfasd中华人民共和fasdfksdjfhlas");
jstr = (*env)->NewStringUTF(env,greeting);
return jstr;
}
在JNI 中将中文字符传给VC,VC不能正确显示(printf).
VC将中文字符返回给JAVA时, 在JAVA中也无法显示.
import java.io.*;
import java.util.*;
class JniTest
{
public static native String webcall13(String prompt);
static {
System.loadLibrary("siuweb");
}
public static String GBToUnicode(String strIn)
{
String strOut = null;
if(strIn == null || (strIn.trim()).equals(""))return strIn;
try{
byte[] b = strIn.getBytes("ISO8859_1");
//byte[] b = strIn.getBytes("GB2312");
//byte[] b = strIn.getBytes("UTF8");
//byte[] b = strIn.getBytes("ISO-8859-1");
strOut = new String(b,"GBK");
//strOut = new String(b,"gb2312");
}
catch(Exception e){}
return strOut;
}
public static String UnicodeToGB(String strIn)
{
byte[] b;
String strOut = null;
if(strIn == null || (strIn.trim()).equals(""))
return strIn;
try{
b = strIn.getBytes("GBK");
strOut = new String(b,"ISO8859_1");
}catch(UnsupportedEncodingException e){}
return strOut;
}
public static void main(String[] args)
{
JniTest test = new JniTest();
//System.getProperties().list(System.out);
test.webcall1();
int count = test.webcall2(2,5);
String str_tmp;
str_tmp = "JA你webcall3:";
String str_input;
str_input = UnicodeToGB(str_tmp);
String input = test.webcall13(str_input);
/* System.out.println("C return string : " + input); */
String str_output;
str_output = GBToUnicode(input);
System.out.println("C return Unicode string : " + str_output);
}
}
JNIEXPORT jstring JNICALL
Java_JniTest_webcall13 (JNIEnv *env, jclass obj, jstring a)
{
jstring jstr;
char greeting[100];
const char *str = (*env)->GetStringUTFChars(env,a,0);
wprintf(L"C wprintf java string: %sn", str);
printf("nC printf java string: %sn", str);
(*env)->ReleaseStringUTFChars(env, a, str);
greeting[0]='';
strcpy(greeting,"abk中cedasdfasd中华人民共和fasdfksdjfhlas");
jstr = (*env)->NewStringUTF(env,greeting);
return jstr;
}
|
不是解决了吗。