WINDOWS CE下ANSI 与 Unicode 字符相互转换

一直在VC下做管了 ANSI 字符处理!在EVC下写出的字符处理程序老有问题!虽然知道是字符问题,但总是有些晕!~所以写一下以免自己再晕!同时也以免初学者晕!~~

在EVC中的字符都是宽(Unicode )字符的!例如,一个EDIT控件的对象m_Info要设置其中的内容要写成以下形式:m_Info.SetWindowText(_T("Hello"));才可以!要使用宏_T()。

但是宏_T()在使用中可能会出现问题(我已经遇到了!我现在还不能判断是我程序的问题还是这个宏有一定的局限性),微软还提供了ANSI 到 Unicode 字符串和 Unicode 字符串转换为 ANSI 的函数MultiByteToWideChar 和 WideCharToMultiByte。我以前在WINDOWS 2000下做驱动时用到过这两个函数!当时是这样处理的!


//宽字符串转换为普通字符串
PCHAR WideStrToMultiStr (PWCHAR WideStr)
{
    ULONG nBytes;
    PCHAR MultiStr;
    // 得到宽字符串的长度
    nBytes = WideCharToMultiByte(
                 CP_ACP,
                 0,
                 WideStr,
                 -1,
                 NULL,
                 0,
                 NULL,
                 NULL);
    if (nBytes == 0)
    {
        return NULL;
    }

    // 为转换后的字符串分配空间
    MultiStr = (PCHAR)GlobalAlloc(GPTR,nBytes);
    if (MultiStr == NULL)
    {
        return NULL;
    }
    // 转换字符串
    nBytes = WideCharToMultiByte(
                 CP_ACP,
                 0,
                 WideStr,
                 -1,
                 MultiStr,
                 nBytes,
                 NULL,
                 NULL);
    if (nBytes == 0)
    {
        GlobalFree(MultiStr);
        return NULL;
    }

    return MultiStr;
}

//普通字符串转换为宽字符串
PWCHAR MultiStrToWideStr (PCHAR MultiStr)
{
    ULONG nBytes;
    PWCHAR WideStr;
    // 得到宽字符串的长度
    nBytes = MultiByteToWideChar(
                 CP_ACP,
                 0,
                 MultiStr,
                 -1,
                 NULL,
                 0
                 );
    if (nBytes == 0)
    {
        return NULL;
    }

    // 为转换后的字符串分配空间
    WideStr = (PWCHAR)GlobalAlloc(GPTR,nBytes);
    if (WideStr == NULL)
    {
        return NULL;
    }
    // 转换字符串
    nBytes = MultiByteToWideChar(
                 CP_ACP,
                 0,
                 MultiStr,
                 -1,
                 WideStr,
                 nBytes
     );
    if (nBytes == 0)
    {
        GlobalFree(WideStr);
        return NULL;
    }

    return WideStr;
}

这两个函数分别实现宽字符串转换为普通字符串和普通字符串转换为宽字符串,我以前在WINDOWS 2000下用的时侯没有问题!现在在WINDOWS CE下用的时候出现异常!最后在微软网站中找到一篇“从 ANSI 到 Unicode & Unicode 到 ANSI 用于 OLE 如何转换”()其中是这样处理的:

/*
* AnsiToUnicode converts the ANSI string pszA to a Unicode string
* and returns the Unicode string through ppszW. Space for the
* the converted string is allocated by AnsiToUnicode.
*/

HRESULT __fastcall AnsiToUnicode(LPCSTR pszA, LPOLESTR* ppszW)
{

    ULONG cCharacters;
    DWORD dwError;

    // If input is null then just return the same.
    if (NULL == pszA)
    {
        *ppszW = NULL;
        return NOERROR;
    }

    // Determine number of wide characters to be allocated for the
    // Unicode string.
    cCharacters =  strlen(pszA)+1;

    // Use of the OLE allocator is required if the resultant Unicode
    // string will be passed to another COM component and if that
    // component will free it. Otherwise you can use your own allocator.
    *ppszW = (LPOLESTR) CoTaskMemAlloc(cCharacters*2);
    if (NULL == *ppszW)
        return E_OUTOFMEMORY;

    // Covert to Unicode.
    if (0 == MultiByteToWideChar(CP_ACP, 0, pszA, cCharacters,
                  *ppszW, cCharacters))
    {
        dwError = GetLastError();
        CoTaskMemFree(*ppszW);
        *ppszW = NULL;
        return HRESULT_FROM_WIN32(dwError);
    }

    return NOERROR;

}
/*
* UnicodeToAnsi converts the Unicode string pszW to an ANSI string
* and returns the ANSI string through ppszA. Space for the
* the converted string is allocated by UnicodeToAnsi.
*/

HRESULT __fastcall UnicodeToAnsi(LPCOLESTR pszW, LPSTR* ppszA)
{

    ULONG cbAnsi, cCharacters;
    DWORD dwError;

    // If input is null then just return the same.
    if (pszW == NULL)
    {
        *ppszA = NULL;
        return NOERROR;
    }

    cCharacters = wcslen(pszW)+1;
    // Determine number of bytes to be allocated for ANSI string. An
    // ANSI string can have at most 2 bytes per character (for Double
    // Byte Character Strings.)
    cbAnsi = cCharacters*2;

    // Use of the OLE allocator is not required because the resultant
    // ANSI  string will never be passed to another COM component. You
    // can use your own allocator.
    *ppszA = (LPSTR) CoTaskMemAlloc(cbAnsi);
    if (NULL == *ppszA)
        return E_OUTOFMEMORY;

    // Convert to ANSI.
    if (0 == WideCharToMultiByte(CP_ACP, 0, pszW, cCharacters, *ppszA,
                  cbAnsi, NULL, NULL))
    {
        dwError = GetLastError();
        CoTaskMemFree(*ppszA);
        *ppszA = NULL;
        return HRESULT_FROM_WIN32(dwError);
    }
    return NOERROR;

}
在WINDWOS CE 下使用正常!

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
可以使用 Unicode 字符集中数字字符的范围来判断一个 Unicode 字符串是否为数字。在 Unicode 字符集中,数字字符的范围是 U+0030 到 U+0039,U+0660 到 U+0669,U+06F0 到 U+06F9,U+07C0 到 U+07C9,U+0966 到 U+096F,U+09E6 到 U+09EF,U+0A66 到 U+0A6F,U+0AE6 到 U+0AEF,U+0B66 到 U+0B6F,U+0BE6 到 U+0BEF,U+0C66 到 U+0C6F,U+0CE6 到 U+0CEF,U+0D66 到 U+0D6F,U+0DE6 到 U+0DEF,U+0E50 到 U+0E59,U+0ED0 到 U+0ED9,U+0F20 到 U+0F29。 以下是一个示例代码,用于判断一个 Unicode 字符串是否为数字: ```c #include <stdio.h> #include <wchar.h> int is_unicode_digit(const wchar_t *str) { while (*str) { wchar_t ch = *str; if (ch < 0x0030 || (ch > 0x0039 && ch < 0x0660) || (ch > 0x0669 && ch < 0x06F0) || (ch > 0x06F9 && ch < 0x07C0) || (ch > 0x07C9 && ch < 0x0966) || (ch > 0x096F && ch < 0x09E6) || (ch > 0x09EF && ch < 0x0A66) || (ch > 0x0A6F && ch < 0x0AE6) || (ch > 0x0AEF && ch < 0x0B66) || (ch > 0x0B6F && ch < 0x0BE6) || (ch > 0x0BEF && ch < 0x0C66) || (ch > 0x0C6F && ch < 0x0CE6) || (ch > 0x0CEF && ch < 0x0D66) || (ch > 0x0D6F && ch < 0x0DE6) || (ch > 0x0DEF && ch < 0x0E50) || (ch > 0x0E59 && ch < 0x0ED0) || (ch > 0x0ED9 && ch < 0x0F20) || ch > 0x0F29) { return 0; } str++; } return 1; } int main() { const wchar_t *str1 = L"1234"; const wchar_t *str2 = L"12.34"; const wchar_t *str3 = L"一二三四"; const wchar_t *str4 = L"1二3四"; printf("%ls is %s digit\n", str1, is_unicode_digit(str1) ? "a" : "not a"); printf("%ls is %s digit\n", str2, is_unicode_digit(str2) ? "a" : "not a"); printf("%ls is %s digit\n", str3, is_unicode_digit(str3) ? "a" : "not a"); printf("%ls is %s digit\n", str4, is_unicode_digit(str4) ? "a" : "not a"); return 0; } ``` 输出结果为: ``` 1234 is a digit 12.34 is not a digit 一二三四 is not a digit 1二3四 is not a digit ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lujunql

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值