最近在用MFC写一个小的工具,从一个读取一个带特殊标签的文件并将里面的标签替换成对应的数据,然后写入另一个生成文件里。由于输入输出文件是Ansi编码的,程序当中CString类存储格式却是UNICODE编码的,所以不得不解决编码转换的问题。
Ansi转UNICODE
这个部分需要调用MutibyteToWideChar函数,参考
http://msdn.microsoft.com/en-us/library/bb202786.aspx
不过有人已经写了可靠的转换函数,建议直接使用下面的这两个函数:
/***************************/
/* ansi-unicode conversion */
/***************************/
BOOL AnsiToUnicode16(CHAR *in_Src, WCHAR *out_Dst, INT in_MaxLen)
{
/* locals */
INT lv_Len;
// do NOT decrease maxlen for the eos
if (in_MaxLen <= 0)
return FALSE;
// let windows find out the meaning of ansi
// - the SrcLen=-1 triggers MBTWC to add a eos to Dst and fails if MaxLen is too small.
// - if SrcLen is specified then no eos is added
// - if (SrcLen+1) is specified then the eos IS added
lv_Len = MultiByteToWideChar(CP_ACP, 0, in_Src, -1, out_Dst, in_MaxLen);
// validate
if (lv_Len < 0)
lv_Len = 0;
// ensure eos, watch out for a full buffersize
// - if the buffer is full without an eos then clear the output like MBTWC does
// in case of too small outputbuffer
// - unfortunately there is no way to let MBTWC return shortened strings,
// if the outputbuffer is too small then it fails completely
if (lv_Len < in_MaxLen)
out_Dst[lv_Len] = 0;
else if (out_Dst[in_MaxLen-1])
out_Dst[0] = 0;
// done
return TRUE;
}
BOOL AnsiToUnicode16L(CHAR *in_Src, INT in_SrcLen, WCHAR *out_Dst, INT in_MaxLen)
{
/* locals */
INT lv_Len;
// do NOT decrease maxlen for the eos
if (in_MaxLen <= 0)
return FALSE;
// let windows find out the meaning of ansi
// - the S