Android的字符编码转换问题,Unicode,GB2312,UTF8等

来源:互联网 发布:俄罗斯方舟 知乎 编辑:程序博客网 时间:2024/05/22 11:47
  1. /* jernymy 2011-12-01
  2. * Android word encoder process, need libicuuc.so Api
  3. * just a example
  4. **/ 
  5.  
  6. /*--------------------------- tst.cpp ---------------------------*/ 
  7. /* use dl API include file */ 
  8. #include <dlfcn.h> 
  9.  
  10. /* typedef a function pointer to pointer ucnv_convert method */ 
  11.  
  12. #ifndef LPCSTR 
  13. typedef constchar*     LPCSTR
  14. #endif 
  15.  
  16. #ifndef LPSTR 
  17. typedef       char*    LPSTR
  18. #endif 
  19.  
  20. #ifndef s32 
  21. typedef unsigned long   s32; 
  22. #endif 
  23.  
  24. typedef void (*pvUcnvFunc) 
  25.     (LPCSTR lpcstrDstEcd,LPCSTR lpcstrSrcEcd, 
  26.      LPSTR lpstrOut, s32 nOutLen, 
  27.      LPCSTR lpstrIn, s32 nInLen, s32 *pnErrCode); 
  28.  
  29. /* ucnv_convert method pointer */ 
  30. static pvUcnvFunc g_pvUcnvConvert = NULL; 
  31.  
  32. /* pointer libicuuc.so dl lib */ 
  33. static void*      g_pvUcnvDll = NULL; 
  34.  
  35. /*
  36. see the source code define
  37. int32_t  ucnv_convert(  const char *toConverterName,
  38.                         const char *fromConverterName,
  39.                         char *target,
  40.                         int32_t targetSize,
  41.                         const char *source,
  42.                         int32_t sourceSize,
  43.                         UErrorCode * err)
  44. */ 
  45.  
  46. void UcnvConvert(LPSTR lpstrOut, s32 nOutLen,LPCSTR lpstrIn, s32 *pnErrC) 
  47.     /* load so for word convert */ 
  48.     if (NULL == g_pvUcnvDll) 
  49.     { 
  50.         g_pvUcnvDll = dlopen("/system/lib/libicuuc.so", RTLD_LAZY);   
  51.     } 
  52.      
  53.     if (NULL == g_pvUcnvDll) 
  54.     { 
  55.         LOGE("(NULL == g_pvUcnvDll)"); 
  56.         return
  57.     } 
  58.      
  59.     /* get convert Api pointer */ 
  60.     if (NULL == g_pvUcnvConvert) 
  61.     { 
  62.         /* here is Android 2.2 version, Android 2.1 version change to ucnv_convert_3_8 */ 
  63.         g_pvUcnvConvert = (pvUcnvFunc)dlsym(g_pvUcnvDll, "ucnv_convert_4_2"); 
  64.     } 
  65.     if (NULL == g_pvUcnvConvert) 
  66.     { 
  67.         LOGE("(NULL == g_pvUcnvConvert)"); 
  68.         return
  69.     } 
  70.      
  71.     /*
  72.      * utf8   --> the Destination encoder
  73.      * gb2312 --> the Source      encoder
  74.     **/ 
  75.     g_pvUcnvConvert("utf8", "gb2312", lpstrOut, nOutLen, lpstrIn, strlen(lpstrIn), pnErrC);   
  76. /*--------------------------- tst.cpp ---------------------------*/ 
  77.  
  78.  
  79. /*--------------------------- Android.mk ---------------------------*/ 
  80. # android for nc makefile 
  81. LOCAL_PATH := $(call my-dir) 
  82. include $(CLEAR_VARS) 
  83.  
  84. # current path, local path jni/ 
  85. COMN_PATH    := $(LOCAL_PATH)/../../../10-common 
  86.  
  87. LOCAL_CFLAGS += -D_LINUX_ -g -fno-rtti -fno-short-enums -D_ANDROID_ 
  88.  
  89. LOCAL_CPP_EXTENSION := .cpp 
  90.  
  91. LOCAL_C_INCLUDES := \ 
  92.  
  93. LOCAL_LDLIBS :=-L$(SYSROOT)/usr/lib -llog -ldl 
  94.  
  95. LOCAL_MODULE    := tst 
  96.  
  97. LOCAL_SRC_FILES := tst.cpp 
  98.  
  99. include $(BUILD_SHARED_LIBRARY) 
  100. /*--------------------------- Android.mk ---------------------------*/ 
原创粉丝点击