The original paste http://topic.csdn.net/u/20101022/16/1b2e0cec-b9d2-42ea-8d9c-4f1bb8320a54.html?r=70149216, saw and hands-on implementation, recorded for reuse.
If it is in the Java layer, there is a string class can be very good conversion of various encodings, under the NDK there is no ready-made public tools, but can be used icu4c.
ICU4C is the C language implementation version of the IBM International development Component ICU. There are also implementations in the Android system. The NDK does not have a publicly available API and needs to load the dynamic library itself to invoke the conversion function.
Android under the ICU Library path is "/system/lib/libicuuc.so", the main use of the conversion function is Ucnv_convert_?_? The question mark here is not the same as the different function names of the versions. In the 2.2 simulator in the libicuuc.so of this function named Ucnv_convert_4_2, in the 2.1 simulator for the Ucnv_convert_3_8, seemingly to be based on different versions to separate treatment, has not found a way to unify.
Function Prototypes:
void Ucnv_convert (const char *, const char *, char *, int32_t, const char *, int32_t,int32_t*);
Usage:
declaring function pointers
void (*ucnv_convert) (const char *, const char *, char *, int32_t, const char *, int32_t,int32_t*) = 0;
Loading the dynamic library
void* PDL = Dlopen ("/system/lib/libicuuc.so", Rtld_lazy);
Take android2.2 as an example, the function name is Ucnv_convert_4_2
Ucnv_convert = (void (*) (const char *, const char *, char *, int32_t, const char *, int32_t,int32_t*)) Dlsym (PDL, "Ucnv_c Onvert_4_2 ");
The load will be successful.
if (Ucnv_convert) {
char* cbuf = "...";
Char buffer[100];
int errcode = 0;
UTF8 is the target encoding, UCS2 is the original character encoding
Buffer is the buffering of the converted characters, given 100 bytes
CBUF is the string pointer to convert
Errcode is an error code and can be searched online
Ucnv_convert ("UTF8", "ucs2", buffer, Cbuf, strlen (cbuf), &errcode);
}
After the successful conversion of the string is placed in buffer, if something goes wrong, it will put the error code inside the Errcode.
As the headline shows, there is also a wide character under the NDK, which is the problem of wchar_t, and porting to other platforms is a hassle.
Linux wchar_t default is 4 bytes, while under Windows (including Ce,mobile) and Symbian are 2 bytes. The workaround is to find the local_cflags to add a compile switch-fshort-wchar (if not written manually) in the Android.mk file, such as local_cflags: =-fshort-wchar. This forces the compiler to handle the wchar_t with 2 bytes, but at compile time there will be warning, regardless.
This way, although the compiler processing into 2 bytes, but the precompiled library libc and so on is still 4 bytes, will cause wcslen and other functions can not be used (in fact, the NDK wcslen is obsolete), the solution can be recompiled libc, but the simplest is to implement wcslen on the line.
The following code is copy on the Web, where the specific forget, you can convert wchar_t to char string, so you can use the ICU library at will to convert.
Get wchar_t string length
int Wlen (const wchar_t* strstring) {
int i=0;
while (1) {
if (strstring[i] = = 0) {
Break
}else{
i++;
}
}
return i;
}
Char *w2c (const wchar_t *PW, char *pc)
{
*pc++ = *PW >> 8;
*PC = *PW;
return 0;
}
Convert string
Char *wstr2cstr (const wchar_t *PWSTR, char *pcstr, size_t len) {
char *ptemp = PCSTR;
if (pwstr! = NULL && PCSTR! = null) {
size_t Wstr_len = Wlen (PWSTR);
Len = (len > Wstr_len)? Wstr_len:len;
while (len--> 0) {
W2C (Pwstr, PCSTR);
pwstr++;
Pcstr + = 2;
}
*pcstr = '/0 ';
return ptemp;
}
return 0;
}
You can convert it using WSTR2CSTR. There is also a byte order problem, in the W2C function, a wchar_t to whether the char is low in front or high in the front or before the conversion of the code to see before and after the specific treatment.
Wide character encoding conversion under Android NDK and use of ICU libraries (GO)