- /* jernymy 2011-12-01
- * Android word encoder process, need libicuuc.so Api
- * just a example
- **/
- /*--------------------------- tst.cpp ---------------------------*/
- /* use dl API include file */
- #include <dlfcn.h>
- /* typedef a function pointer to pointer ucnv_convert method */
- #ifndef LPCSTR
- typedef const char* LPCSTR;
- #endif
- #ifndef LPSTR
- typedef char* LPSTR;
- #endif
- #ifndef s32
- typedef unsigned long s32;
- #endif
- typedef void (*pvUcnvFunc)
- (LPCSTR lpcstrDstEcd, LPCSTR lpcstrSrcEcd,
- LPSTR lpstrOut, s32 nOutLen,
- LPCSTR lpstrIn, s32 nInLen, s32 *pnErrCode);
- /* ucnv_convert method pointer */
- static pvUcnvFunc g_pvUcnvConvert = NULL;
- /* pointer libicuuc.so dl lib */
- static void* g_pvUcnvDll = NULL;
- /*
- see the source code define
- int32_t ucnv_convert( const char *toConverterName,
- const char *fromConverterName,
- char *target,
- int32_t targetSize,
- const char *source,
- int32_t sourceSize,
- UErrorCode * err)
- */
- void UcnvConvert(LPSTR lpstrOut, s32 nOutLen, LPCSTR lpstrIn, s32 *pnErrC)
- {
- /* load so for word convert */
- if (NULL == g_pvUcnvDll)
- {
- g_pvUcnvDll = dlopen("/system/lib/libicuuc.so", RTLD_LAZY);
- }
- if (NULL == g_pvUcnvDll)
- {
- LOGE("(NULL == g_pvUcnvDll)");
- return;
- }
- /* get convert Api pointer */
- if (NULL == g_pvUcnvConvert)
- {
- /* here is Android 2.2 version, Android 2.1 version change to ucnv_convert_3_8 */
- g_pvUcnvConvert = (pvUcnvFunc)dlsym(g_pvUcnvDll, "ucnv_convert_4_2");
- }
- if (NULL == g_pvUcnvConvert)
- {
- LOGE("(NULL == g_pvUcnvConvert)");
- return;
- }
- /*
- * utf8 --> the Destination encoder
- * gb2312 --> the Source encoder
- **/
- g_pvUcnvConvert("utf8", "gb2312", lpstrOut, nOutLen, lpstrIn, strlen(lpstrIn), pnErrC);
- }
- /*--------------------------- tst.cpp ---------------------------*/
- /*--------------------------- Android.mk ---------------------------*/
- # android for nc makefile
- LOCAL_PATH := $(call my-dir)
- include $(CLEAR_VARS)
- # current path, local path jni/
- COMN_PATH := $(LOCAL_PATH)/../../../10-common
- LOCAL_CFLAGS += -D_LINUX_ -g -fno-rtti -fno-short-enums -D_ANDROID_
- LOCAL_CPP_EXTENSION := .cpp
- LOCAL_C_INCLUDES := \
- LOCAL_LDLIBS :=-L$(SYSROOT)/usr/lib -llog -ldl
- LOCAL_MODULE := tst
- LOCAL_SRC_FILES := tst.cpp
- include $(BUILD_SHARED_LIBRARY)
- /*--------------------------- Android.mk ---------------------------*/
Android的字符编码转换问题,Unicode,GB2312,UTF8等
最新推荐文章于 2024-05-16 10:47:25 发布