為SQLite3提供一個ANSI到UTF8的互轉(zhuǎn)函數(shù)
在使用Sqlite3時必須要用到的
使用方法:
char* src = "...";//待轉(zhuǎn)換的ANSI或UTF8字符串
char* dst = NULL;//保存由函數(shù)內(nèi)部分配的內(nèi)存指針, 不需要傳入內(nèi)存緩沖區(qū)的
轉(zhuǎn)換為UTF-8:to_utf8(src, &dst);
轉(zhuǎn)換為ANSI:to_gb(src, &dst);
返回值:零 - 失敗, 非零 - 成功.
注意:如果操作成功, 需要手動釋放函數(shù)內(nèi)部分配的空間:
if(dst)
{
free(dst);
dst = NULL;
}
代碼:
#include <windows.h>
#include <stdio.h>int to_utf8(char* psrc, char** ppdst)
{
int ret,ret2;
wchar_t* pws = NULL;
char* putf = NULL;
ret = MultiByteToWideChar(CP_ACP, 0, psrc, -1, NULL, 0);
if(ret<=0){
*ppdst = NULL;
return 0;
}
pws = (wchar_t*)malloc(ret*2);
if(!pws){
*ppdst = NULL;
return 0;
}
MultiByteToWideChar(CP_ACP, 0, psrc, -1, pws, ret);
ret2 = WideCharToMultiByte(CP_UTF8, 0, pws, -1, NULL, 0, NULL, NULL);
if(ret2<=0){
free(pws);
return 0;
}
putf = (char*)malloc(ret2);
if(!putf){
free(pws);
return 0;
}
if(WideCharToMultiByte(CP_UTF8, 0, pws, ret, putf, ret2, NULL, NULL)){
*ppdst = putf;
free(pws);
return 1;
}else{
free(pws);
free(putf);
*ppdst = NULL;
return 0;
}
}
int to_gb(char* psrc, char** ppdst)
{
int ret, ret2;
wchar_t* pws = NULL;
char* pgb = NULL;
ret = MultiByteToWideChar(CP_UTF8, 0, psrc, -1, NULL, 0);
if(ret<=0){
*ppdst = NULL;
return 0;
}
pws = (wchar_t*)malloc(ret*2);
if(!pws){
*ppdst = NULL;
return 0;
}
MultiByteToWideChar(CP_UTF8, 0, psrc, -1, pws, ret);
ret2 = WideCharToMultiByte(CP_ACP, 0, pws, -1, NULL, 0, NULL, NULL);
if(ret2<=0){
free(pws);
return 0;
}
pgb = (char*)malloc(ret2);
if(!pgb){
free(pws);
*ppdst = NULL;
return 0;
}
if(WideCharToMultiByte(CP_ACP, 0, pws, -1, pgb, ret2, NULL, NULL)){
*ppdst = pgb;
free(pws);
return 1;
}else{*ppdst = 0;
free(pgb);
free(pws);
return 0;
}
}
by: 女孩不哭
相關(guān)文章
System.Data.SQLite 數(shù)據(jù)庫詳細(xì)介紹
System.Data.SQLite是SQLite的加強(qiáng)版,它可以無需.NET Framework支持,由于它內(nèi)部包含了一個ADO.NET 2.0引擎,所以.NET開發(fā)人員可以利用System.Data.SQLite方便地開發(fā).NET程序。2011-02-02保護(hù)你的Sqlite數(shù)據(jù)庫(SQLite數(shù)據(jù)庫安全秘籍)
相信使用PHP開發(fā)的人員一定不會對SQLite感到陌生了,PHP5已經(jīng)集成了這個輕量型的數(shù)據(jù)庫。并且很多虛擬主機(jī)無論是win還是*nux都支持它。2009-08-08Win11下基于VS2022編譯SQLite3源碼的實現(xiàn)步驟
本文主要介紹了Win11下基于VS2022編譯SQLite3源碼的實現(xiàn)步驟,文中通過圖文介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-09-09SQLite學(xué)習(xí)手冊(SQLite在線備份)
在SQLite中提供了一組用于在線數(shù)據(jù)庫備份的APIs函數(shù)(C接口),可以很好的解決上述方法存在的不足。通過該組函數(shù),可以將源數(shù)據(jù)庫中的內(nèi)容拷貝到另一個數(shù)據(jù)庫,同時覆蓋目標(biāo)數(shù)據(jù)庫中的數(shù)據(jù)2013-12-12SQLite教程(五):數(shù)據(jù)庫和事務(wù)
這篇文章主要介紹了SQLite教程(五):數(shù)據(jù)庫和事務(wù),本文講解了Attach數(shù)據(jù)庫、Detach數(shù)據(jù)庫、事務(wù)等內(nèi)容,需要的朋友可以參考下2015-05-05SQLite數(shù)據(jù)庫常用語句及MAC上的SQLite可視化工具M(jìn)easSQLlite使用方法
這篇文章主要介紹了SQLite數(shù)據(jù)庫常用語句及MAC上的SQLite可視化工具M(jìn)easSQLlite使用方法,需要的朋友可以參考下2016-01-01