国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術(shù)文章
文章詳情頁

為SQLite3提供一個ANSI到UTF8的互轉(zhuǎn)函數(shù)

瀏覽:691日期:2023-04-05 14:56:10

在使用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)部分配的空間:

復(fù)制代碼 代碼如下:
if(dst)
{
    free(dst);
    dst = NULL;
}

代碼:

復(fù)制代碼 代碼如下:
#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: 女孩不哭

標(biāo)簽: SQLite
相關(guān)文章:
主站蜘蛛池模板: 平武县| 铅山县| 连云港市| 五峰| 红原县| 台南市| 竹北市| 襄樊市| 喀喇沁旗| 安庆市| 弥渡县| 乌兰浩特市| 荥经县| 通渭县| 郯城县| 湘潭市| 宁夏| 米泉市| 绥阳县| 罗山县| 沧源| 原阳县| 常德市| 洱源县| 南康市| 普宁市| 聊城市| 平武县| 平舆县| 四会市| 余江县| 渭源县| 贡嘎县| 樟树市| 和田县| 合肥市| 开平市| 龙江县| 平乐县| 桃源县| 建德市|