|
|
|
|||||
|
Encoding Problem about Chinese, client compiled by Java, server compiled by C++
a project server compiled by C++ and client compiled by java for display, but a problem comes when Chinese display.
when I compiled client by C++, the Chinese display is normal, but when I compiled client by java, the Chinese display isn't normal, that's disorder. Now I solved it by update the source, under directory "src/IceInternal". If there others way solve it, such as update config etc.??
__________________
Wireless Support Platform --------------------------------------------- Yanbo Ma Central University for Nationality, P.R.China http://www.cun.edu.cn yanboma_bj@yahoo.com.cn --------------------------------------------- |
|
|||||
|
my code as follows:
ice definition:
#ifndef HELLO_ICE #define HELLO_ICE module Demo { interface Hello { nonmutating void sayHello(); }; }; #endif C++ implement server: HelloI::sayHello(const Ice::Current&) const { cout << "Hello World!哈哈哈哈哈哈哈" << endl; } when java client call 'sayHello' on windows the result is: Hello World!????????? The source I update is: the directory: IceJ-2.1.2/src/IceInternal the file :BasicStream.java I note the 'UTF8' encode is the main modification. the result of differ as follow: diff -r IceJ-2.1.2.1/src/IceInternal/BasicStream.java IceJ-2.1.2/src/IceInternal/BasicStream.java 1038c1038 < // try --- > try 1040,1041c1040 < //byte[] arr = v.getBytes("UTF8"); < byte[] arr = v.getBytes(); --- > byte[] arr = v.getBytes("UTF8"); 1046,1049c1045,1048 < // catch(java.io.UnsupportedEncodingException ex) < // { < // assert(false); < // } --- > catch(java.io.UnsupportedEncodingException ex) > { > assert(false); > } 1121,1122c1120 < //return new String(_stringBytes, 0, len, "UTF8"); < return new String(_stringBytes, 0, len); --- > return new String(_stringBytes, 0, len, "UTF8"); 1131c1129 < /* catch(java.io.UnsupportedEncodingException ex) --- > catch(java.io.UnsupportedEncodingException ex) 1136c1134 < */ catch(java.nio.BufferUnderflowException ex) --- > catch(java.nio.BufferUnderflowException ex)
__________________
Wireless Support Platform --------------------------------------------- Yanbo Ma Central University for Nationality, P.R.China http://www.cun.edu.cn yanboma_bj@yahoo.com.cn --------------------------------------------- |
|
||||||
|
Is this really the code you're using? The code you posted doesn't receive a string over the wire, it just prints a string on the standard error output so I don't see how invoking the sayHello() method would print different strings if invoked from a C++ client or Java client.
It's also not quite clear from the diff output what are the changes you made. Could you just post your BasicStream.java file with your modifications as an attachment? Benoit. |
|
|||||
|
Because the code I using is much, I didn't paste it. I only want to explain the phenomenon that display the Chinese through the code as above, it isn't the code I using.
the BasicStream.java I modified as follow: public void writeString(String v) { if(v == null) { writeSize(0); } else { final int len = v.length(); if(len > 0) { // try { //byte[] arr = v.getBytes("UTF8"); byte[] arr = v.getBytes(); writeSize(arr.length); expand(arr.length); _buf.put(arr); } // catch(java.io.UnsupportedEncodingException ex) // { // assert(false); // } } else { writeSize(0); } } } public void writeStringSeq(String[] v) { if(v == null) { writeSize(0); } else { writeSize(v.length); for(int i = 0; i < v.length; i++) { writeString(v[i]); } } } public String readString() { final int len = readSize(); if(len == 0) { return ""; } else { try { // // We reuse the _stringBytes array to avoid creating // excessive garbage // if(_stringBytes == null || len > _stringBytes.length) { _stringBytes = new byte[len]; _stringChars = new char[len]; } _buf.get(_stringBytes, 0, len); // // It's more efficient to construct a string using a // character array instead of a byte array, because // byte arrays require conversion. // for(int i = 0; i < len; i++) { if(_stringBytes[i] < 0) { // // Multi-byte character found - we must use // conversion. // // TODO: If the string contains garbage bytes // that won't correctly decode as UTF, the // behavior of this constructor is // undefined. It would be better to explicitly // decode using // java.nio.charset.CharsetDecoder and to // throw MarshalException if the string won't // decode. // //return new String(_stringBytes, 0, len, "UTF8"); return new String(_stringBytes, 0, len); } else { _stringChars[i] = (char)_stringBytes[i]; } } return new String(_stringChars, 0, len); } /* catch(java.io.UnsupportedEncodingException ex) { assert(false); return ""; } */ catch(java.nio.BufferUnderflowException ex) { throw new Ice.UnmarshalOutOfBoundsException(); } } }
__________________
Wireless Support Platform --------------------------------------------- Yanbo Ma Central University for Nationality, P.R.China http://www.cun.edu.cn yanboma_bj@yahoo.com.cn --------------------------------------------- |
|
||||||
|
Since the string displayed by your C++ program is not transmitted by Ice, Ice is not involved. And your change to the Ice source code does not help.
In C++, to display wide-strings, you should use wcout, e.g.: wcout << L"Hello World [chinese characters]" << endl; Cheers, Bernard |
|
|||||
|
Re
Quote:
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Pre-Compiled Headers | mario | Help Center | 9 | 11-10-2005 04:22 AM |
| encoding error when Ice java client get string from Ice c++ server | casper | Help Center | 1 | 09-16-2005 04:01 AM |
| Ice and native compiled Java (GJC) | taweili | Help Center | 0 | 02-02-2004 09:20 PM |
| Can Ice be compiled with gcc 2.95? | jukvaini | Help Center | 5 | 04-07-2003 05:18 AM |