IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    常用的字符串处理函数

    天下发表于 2015-12-14 12:33:00
    love 0
    ◆ 常用的字符串处理函数,具体信息见MSDN:

    字符串长度:
    ● Ansi:strlen(char *str);                
    ● Unicode:wcslen(wchar_t *str);
    ● 通用函数:_tcslen(TCHAR *str);

     

    ● Ansi:int atoi(const char *str);            
    ● Unicode:int _wtoi(const wchar_t *str);
    ● 通用函数:_tstoi(const TCHAR *str);

     

    字符串拷贝:
    ● Ansi:strcpy(char *strDestination, const char *strSource); 
    ● Unicode:wcscpy(wchar_t *strDestination, const wchar_t *strSource);
    ● 通用函数:_tcscpy(TCHAR *strDestination, const TCHAR *strSource);
     

    以上函数不安全,在vs2003等以上版本的编译器中会有warnning警告提示,以下为安全函数(vc++6.0不支持):
    ● Ansi:strcpy_s(char *strDestination, size_t numberOfElements, const char *strSource);
    ● Unicode:wcscpy_s(wchar_t *strDestination, size_t numberOfElements, const wchar_t *strSource);
    ● 通用函数:_tcscpy_s(TCHAR *strDestination, size_t numberOfElements, const TCHAR *strSource);
     

    numberOfElements Size of the destination string buffer. 目的缓冲区的大小,以字节为单位,不是字符!

     

    size_t unsigned integer,在MSDN中的解释:Result of sizeof operator,也就是说 size_t 是 unsigned integer 即无符号整数。那为什么会有size_t这个类型呢? 因为不同平台的操作系统(32/64)中 int/long 等类型所占的字节并不一样,而 size_t 在不同的平台下有不同的定义。有点类似于TCHAR类型:

    #ifndef   _SIZE_T_DEFINED  
      #ifdef     _WIN64  
      typedef   unsigned   __int64         size_t;   //8个字节,64位 
      #else  
      typedef   _W64   unsigned   int       size_t;   //4个字节,32位  
      #endif  
      #define   _SIZE_T_DEFINED
    #endif
     

    ◆转换用到的最根本的 API 函数:
    WideCharToMultiByte 实现宽字节转换到窄字节          //函数参数自行查询MSDN。
    MultiByteToWideChar 实现窄字节转换到宽字节          

    WideCharToMultiByte 的代码页用来标记与新转换的字符串相关的代码页;
    MultiByteToWideChar 的代码页用来标记与一个多字节字符串相关的代码页,

    [1]、常用的代码页有 CP_ACP 和 CP_UTF8 两个: 使用 CP_ACP 代码页就实现了 ANSI 与 Unicode 之间的转换;
        使用 CP_UTF8 代码页就实现了 UTF-8 与 Unicode 之间的转换。
    [2]、dwFlags 参数允许我们进行额外的控制,但是,一般情况下都不使用这个标志,直接传递 0 就行了。
    [3]、lpDefaultChar和pfUsedDefaultChar:
    只有当WideCharToMultiByte函数遇到一个宽字节字符,而该字符在uCodePage参数标识的代码页中并没有它的表示法时,WideCharToMultiByte函数才使用这两个参数。如果宽字节字符不能被转换,该函数便使用lpDefaultChar参数指向的字符。如果该参数是NULL(这是大多数情况下的参数值),那么该函数使用系统的默认字符。该默认字符通常是个问号。这对于文件名来说是危险的,因为问号是个通配符。pfUsedDefaultChar参数指向一个布尔变量,如果Unicode字符串中至少有一个字符不能转换成等价多字节字符,那么函数就将该变量置为TRUE。如果所有字符均被成功地转换,那么该函数就将该变量置为FALSE。当函数返回以便检查宽字节字符串是否被成功地转换后,可以测试该变量。

    ● 两个转换函数的使用举例:
    char *cctryWideCharToAnsi(wchar_t *pWideChar)
    {
        if (!pWideChar) return NULL;
        char *pszBuf = NULL;
        int needBytes = WideCharToMultiByte(CP_ACP, 0, pWideChar, -1, NULL, 0, NULL, NULL);
        if (needBytes > 0){
            pszBuf = new char[needBytes+1];
            ZeroMemory(pszBuf, (needBytes+1)*sizeof(char));
            WideCharToMultiByte(CP_ACP, 0, pWideChar, -1, pszBuf, needBytes, NULL, NULL);
        }
        return pszBuf;
    }
     
    wchar_t *cctryAnsiCharToWide(char *pChar)
    {
        if (!pChar) return NULL;
        wchar_t *pszBuf = NULL;
        int needWChar = MultiByteToWideChar(CP_ACP, 0, pChar, -1, NULL, 0);
        if (needWChar > 0){
            pszBuf = new wchar_t[needWChar+1];
            ZeroMemory(pszBuf, needWChar+1);
            MultiByteToWideChar(CP_ACP, 0, pChar, -1, pszBuf, needWChar);
        }
     
        return pszBuf;
    }
    使用过后千万别忘记释放空间


    天下 2015-12-14 20:33 发表评论


沪ICP备19023445号-2号
友情链接