Unicode与ANSI之间的字符串转换

//==============本文参考Windows核心编程

1、MultiByteToWideChar 用于将多字节字符串转换成宽字符串

int MultiByteToWideChar(
    UINT CodePage,          //code page
    DWORD dwFlags,          //character-type options
    LPCSTR lpMultiByteStr,  //address of string to map
    int cchMultiByte,       //number of bytes in string
    LPWSTR lpWideCharStr,   //address of wide-character buffer
    int cchWideChar         //size of buffer
);

uCodePage参数用于标识一个与多字节字符串相关的代码页号。
dwFlags参数用于设定另一个控件,它可以用重音符号之类的区分标记来影响字符。这些标志通常并不使用,在dwFlags参数中传递0 。
pMultiByteStr参数用于设定要转换的字符串,
cchMultiByte参数用于指明该字符串的长度(按字节计算)。如果为cchMultiByte参数传递-1 ,那么该函数用于确定源字符串的长度。
转换后产生的Unicode版本字符串将被写入内存中的缓存,其地址由
pWideCharStr参数指定。必须在
cchWideChar参数中设定该缓存的最大值(以字符为计量单位)。如果调用MultiByteToWideChar,给cchWideChar参数传递0 ,那么该参数将不执行字符串的转换,而是返回为使转换取得成功所需要的缓存的值。一般来说,可以通过下列步骤将多字节字符串转换成Unicode等价字符串:
1) 调用MultiByteToWideChar函数,为pWideCharStr 参数传递NULL ,为cchWideChar参数传递0 。
2) 分配足够的内存块,用于存放转换后的Unicode字符串。该内存块的大小由前面对MultByteToWideChar的调用返回。
3) 再次调用MultiByteToWideChar,这次将缓存的地址作为pWideCharStr参数来传递,并传递第一次调用MultiByteToWideChar时返回的缓存大小,作为cchWidechar参数。
4. 使用转换后的字符串。
5) 释放Unicode字符串占用的内存块。

2、WideCharToMultiByte函数用于将宽字符串转换成等价的多字节字符串

int WideCharToMultiByte(
  UINT CodePage,         // code page
  DWORD dwFlags,         // performance and mapping flags
  LPCWSTR lpWideCharStr, // address of wide-character string
  int cchWideChar,       // number of characters in string
  LPSTR lpMultiByteStr,  // address of buffer for new string
  int cchMultiByte,      // size of buffer
  LPCSTR lpDefaultChar,  // address of default for unmappable 
                         // characters
  LPBOOL lpUsedDefaultChar   // address of flag set when default 
                             // char. used
);

该函数与MultiBiteToWideChar函数相似。同样,uCodePage参数用于标识与新转换的字符串相关的代码页。
dwFlags则设定用于转换的其他控件。这些标志能够作用于带有区分符号的字符和系统不能转换的字符。通常不需要为字符串的转换而拥有这种程度的控制手段,你将为dwFlags 参数传递0 。
pWideCharStr参数用于设定要转换的字符串的内存地址,
cchWideChar参数用于指明该字符串的长度(用字符数来计量)。如果你为cchWideChar参数传递-1 ,那么该函数用于确定源字符串的长度。
转换产生的多字节版本的字符串被写入由pMultiByteStr参数指明的缓存。必须在cchMultiByte参数中设定该缓存的最大值(用字节来计量)。如果传递0 作为WideCharToMultiByte 函数的cchMultiByte 参数,那么该函数将返回目标缓存需要的大小值。通常可以使用将多字节字符串转换成宽字节字符串时介绍的一系列类似的事件,将宽字节字符串转换成多字节字符串。
你会发现,WideCharToMultiByte函数接受的参数比MultiByteToWideChar函数要多2个,即pDefaultChar和pfUsedDefaultChar。只有当WideCharToMultiByte 函数遇到一个宽字节字符,而该字符在uCodePage参数标识的代码页中并没有它的表示法时,WideCharToMultiByte函数才使用这两个参数。如果宽字节字符不能被转换,该函数便使用pDefaultChar参数指向的字符。如果该参数是NULL(这是大多数情况下的参数值),那么该函数使用系统的默认字符。该默认字符通常是个问号。这对于文件名来说是危险的,因为问号是个通配符。
pfUsedDefaultChar参数指向一个布尔变量,如果宽字符串中至少有一个字符不能转换成等价多字节字符,那么函数就将该变量置为TRUE 。如果所有字符均被成功地转换,那么该函数就将该变量置为FALSE 。当函数返回以便检查宽字节字符串是否被成功地转换后,可以测试该变量。同样,通常为该测试传递NULL。

例如可以编写一个动态链接库,它包含一个函数,能够转换字符串中的所有字符。可以像下面这样编写该函数的Unicode版本

BOOL StringReverseW(PWSTR pWideCharStr)
{
   //Get a pointer to the last character in the string.
   PWSTR pEndOfStr=pWideCharStr+wcslen(pWideCharStr)-1;
   wchar_t cCharT;

   //Repeat until we reach the center character
   //in the string.
   while (pWideCharStr < pEndOfStr)
   {
      //Save a character in a temporary variable.
      cCharT=*pWideCharStr;
      
      //Put the last character in the first character.
      *pWideCharStr =*pEndOfStr;
   
      //Put the temporary character in the last character.
      *pEndOfStr=cCharT;
   
      //Move in one character from the left.
      pWideCharStr++;
   
      //Move in one character from the right.
      pEndOfStr--;
   }
   //The string is reversed; return success.
   return(TRUE);
}
可以编写该函数的ANSI版本以便该函数根本不执行转换字符串的实际操作。你也可以编写该函数的ANSI版本,以便该函数它将ANSI字符串转换成Unicode字符串,将Unicode字符串传递给StringReverseW函数,然后将转换后的字符串重新转换成ANSI字符串。例如:
BOOL StringReverseA(PSTR pMultiByteStr) 
{
   PWSTR pWideCharStr;
   int nLenOfWideCharStr;
   BOOL fOk = FALSE;

   //Calculate the number of characters needed to hold
   //the wide_character version of string.
   nLenOfWideCharStr = MultiRyteToWideChar(CP_ACP, 0,
      pMultiByteStr, -1, NULL, 0);

   //Allocate memory from the process's default heap to 
   //accommodate the size of the wide-character string.
   //Don't forget that MultiByteToWideChar returns the 
   //number of characters,not the number of bytes,so
   //you must multiply by the size of wide character.
   pWideCharStr = HeapAlloc(GetProcessHeap(), 0, 
      nLenOfWideCharStr * sizeof(WCHAR));

   if (pWideCharStr == NULL)
      return(fOk);

   //Convert the multibyte string to a wide_character string.
   MultiByteToWideChar(CP_ACP, 0, pMulti8yteStr, -1, 
      pWideCharStr, nLenOfWideCharStr);

   //Call the wide-character version of this 
   //function to do the actual work 
   fOk = StnngReverseW(pWideCharStr);
   if (fOk)
   {
      //Convert the wide-character string back 
      //to a multibyte string.
      WideCharToMultiByte(CP_ACP, 0, pWideCharStr, -1, 
         pMultiByteStr, strlen(pMultiByteStr), NULL, NULL);
   }

   //Free the momory containing the wide-character string.
   HeapFree(GetProcessHeap(), 0, pWideCharStr);

   return(fOk),
}
最后,在用动态链接库分配的头文件中,可以像下面这样建立这两个函数的原型:
BOOL StringReverseW (PWSTR pWideCharStr);
BOOL StringReverseA (PSTR pMultiByteStr);

#ifdef UNICODE
#define StringReverse StringReverseW
#else
#define StringReverse StringReverseA
#endif // UNICODE


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
编译原理是计算机专业的一门核心课程,旨在介绍编译程序构造的一般原理和基本方法。编译原理不仅是计算机科学理论的重要组成部分,也是实现高效、可靠的计算机程序设计的关键。本文将对编译原理的基本概念、发展历程、主要内容和实际应用进行详细介绍编译原理是计算机专业的一门核心课程,旨在介绍编译程序构造的一般原理和基本方法。编译原理不仅是计算机科学理论的重要组成部分,也是实现高效、可靠的计算机程序设计的关键。本文将对编译原理的基本概念、发展历程、主要内容和实际应用进行详细介绍编译原理是计算机专业的一门核心课程,旨在介绍编译程序构造的一般原理和基本方法。编译原理不仅是计算机科学理论的重要组成部分,也是实现高效、可靠的计算机程序设计的关键。本文将对编译原理的基本概念、发展历程、主要内容和实际应用进行详细介绍编译原理是计算机专业的一门核心课程,旨在介绍编译程序构造的一般原理和基本方法。编译原理不仅是计算机科学理论的重要组成部分,也是实现高效、可靠的计算机程序设计的关键。本文将对编译原理的基本概念、发展历程、主要内容和实际应用进行详细介绍编译原理是计算机专业的一门核心课程,旨在介绍编译程序构造的一般原理和基本

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值