Unicode与ANSI字符串转换

Windows函数MultiByteToWideChar将多字节字符串转换为宽字符字符串。

int
WINAPI
MultiByteToWideChar(
    __in UINT     CodePage,
    __in DWORD    dwFlags,
    __in LPCSTR   lpMultiByteStr,
    __in int      cbMultiByte,
    __out_ecount_opt(cchWideChar) LPWSTR  lpWideCharStr,
    __in int      cchWideChar);


参数: CodePage

//
//  Code Page Default Values.
//
#define CP_ACP                    0           // default to ANSI code page
#define CP_OEMCP                  1           // default to OEM  code page
#define CP_MACCP                  2           // default to MAC  code page
#define CP_THREAD_ACP             3           // current thread's ANSI code page
#define CP_SYMBOL                 42          // SYMBOL translations


#define CP_UTF7                   65000       // UTF-7 translation
#define CP_UTF8                   65001       // UTF-8 translation

参数 dwFlags:参数允许我们进行额外的控制,它会影响带变音符号(比如重音)的字符,但是,一般情况下不使用, 所以dwFlags一般为 0.


参数:lpMultiByteStr 多字节字符的地址。

参数 :cbMultiByte多字节字符串的长度(字节数)。如果 传给cbMultiByte参数是-1,函数便可自动判断字符串的长度。

参数 : lpWideCharStr 是宽字符的存放地址

参数 : cchWideChar是宽字符的长度。


再使用的时候,

两次调用 MultiByteToWideChar();

第一次调用的时候,

                   int dwLength= MultiByteToWideChar(CP_ACP,0,chMax, -1, NULL,0);

wChar chNewWide[dwLength];

MultiByteToWideChar(CP_ACP, 0 , chMax, -1, chNewWide, sizeof(WCHAR) *dwLength);


Next: 来讨论 WideCharToMultiByte这个函数


int
WINAPI
WideCharToMultiByte(
    __in UINT     CodePage,
    __in DWORD    dwFlags,
    __in_opt LPCWSTR  lpWideCharStr,
    __in int      cchWideChar,
    __out_bcount_opt(cbMultiByte) LPSTR   lpMultiByteStr,
    __in int      cbMultiByte,
    __in_opt LPCSTR   lpDefaultChar,
    __out_opt LPBOOL  lpUsedDefaultChar);

这两个 函数差不多的。就不再解释了,直接写程序。

dwLength = WideCharToMultiByte(CP_ACP, 0, chC, -1, NULL, 0, NULL, NULL);
char *chD = NULL;
chD = (char *)malloc(dwLength);
dwLength = WideCharToMultiByte(CP_ACP, 0, chC, -1, chD, dwLength, NULL, NULL );今天先讲到这里吧。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值