http://blog.csdn.net/serverxp/article/details/5538017
最近在做一套跨平臺的短信收發開發程序,遇到了一個問題,那就是文字編碼轉換。在windowsg下的轉換有庫函數
MultiByteToWideChar WideCharToMultiByte,這二個,但是我要的是在linux機器下也可以正常使用,所以google了一天,發現了二個解決方案,一個是libiconv,一個就是ICU了,實際使用后,發現還是ICU更好用,下面是一個簡單的例子。
- #include <unicode/ucnv.h>
- #ifdef _WIN32
- #pragma comment(lib, "icuuc.lib")
- #endif
- //返回0為成功,錯誤代碼定義見后面
- int convert(const char * toConverterName,
- const char * fromConverterName,
- char * target,
- int32_t targetCapacity,
- const char * source,
- int32_t sourceLength)
- {
- UErrorCode error = U_ZERO_ERROR;
- ucnv_convert(toConverterName,fromConverterName,target, targetCapacity, source, sourceLength, &error );
- return error;
- }
- int _tmain(int argc, _TCHAR* argv[])
- {
- wchar_t aaa[] = L"中國人的系統上123323";
- int alen = wcslen(aaa);
- int blen=alen*2+sizeof(int);
- char *abuff=new char[blen];
- int result = convert( "gb2312", "utf-16le", abuff, blen, (const char *)aaa, alen);
- cout << abuff << endl<<strlen(abuff)<<endl;
- delete []abuff;
- return 0;
- }