将Unicode转换为ASCII

 

 

     从richEdit控件中 读取到文字,然后将其保存到文件中时,出现乱码

 

    因为从RichEdit中控件中读取到文字为 UNICODE CString类型,   保存到文件时,使用的是CFile::Write() 写入的为ASCII码,因此,必须对编码格式进行转换。

 

    方法一 :使用WideCharToMultiByte

 

 

      CFile file(strPath,CFile::modeCreate|CFile::modeWrite);
      m_RichEdit.GetWindowText(strText);

 

 

下面是将 strText 中的unicode字符串转换成ASC||方法  ,也是CString 转换为char * 的方法

 

 

char *lText;

int iTexLen;

iTextLen=WideCharToMultiByte(CP_ACP,0,strText,-1,NULL,0,NULL,0);/确定strText中的CString转换为ASCII后,所需的字节数

 

 lText=(char*)calloc(iTextLen,sizeof(char)); // 分配空间

 

 memset(lText,0,iTextLen*sizeof(char));  //初始化空间

 

WideCharToMultiByte(CP_ACP,0,strText,-1,lText,iTextLen,NULL,0);  // 将strText中的字符串全部转换成ASCII,并将其保存在lText开辟的字符空间中

 

 file.Write(lText,iTextLen); //  将转换好的ASCII文字串  保存到文件中 

 file.Close();
 free(lText);

 

 

以下是 另存为对话框的代码:

 

CFileDialog dlg(FALSE,NULL,NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,L"All Files(*.*)|*.*||",AfxGetMainWnd()); CString strPath,strText=L""; char write[1000]; if (dlg.DoModal()==IDOK) { strPath=dlg.GetPathName(); if(strPath.Right(4)!=".txt") strPath+=".txt"; } CFile file(strPath,CFile::modeCreate|CFile::modeWrite); m_RichEdit.GetWindowText(strText); char *lText; int iTextLen; iTextLen=WideCharToMultiByte(CP_ACP,0,strText,-1,NULL,0,NULL,0); lText=(char*)calloc(iTextLen,sizeof(char)); memset(lText,0,iTextLen*sizeof(char)); WideCharToMultiByte(CP_ACP,0,strText,-1,lText,iTextLen,NULL,0); file.Write(lText,iTextLen); file.Close(); free(lText);

 

 

 

 

方法二: 使用 USES_CONVERSION 转换宏

 

 

 CFile file(strPath,CFile::modeCreate|CFile::modeWrite);
 m_RichEdit.GetWindowText(strText);


 USES_CONVERSION;

    LPSTR lpText=W2A(strText);     //  将strText字符串转换成ASCII char *类型,字符串以'/0'作为结束

    LPSTR  p=lpText;                      //   指针P用来计算char *字符串的长度
 int length;
 for ( length=0;;length++)           //    计算字符串的长度
 {
  if (*p=='/0')  break;
  p++;

 }

 

 file.Write(lpText,length);    //此处为字节数目length, 而不是字数 strText.GetLength();
 file.Close();

 

 

源代码如下:

 

CFileDialog dlg(FALSE,NULL,NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,L"All Files(*.*)|*.*||",AfxGetMainWnd()); CString strPath,strText=L""; char write[1000]; if (dlg.DoModal()==IDOK) { strPath=dlg.GetPathName(); if(strPath.Right(4)!=".txt") strPath+=".txt"; } CFile file(strPath,CFile::modeCreate|CFile::modeWrite); m_RichEdit.GetWindowText(strText); USES_CONVERSION; LPSTR lpText=W2A(strText); LPSTR p=lpText; int length; for ( length=0;;length++) { if (*p=='/0') { break; } p++; } file.Write(lpText,length); file.Close();

 

 

注: 字节数目 与字数的不同

 

比如 “你好he”    其字数为4, 但其字节数为6(汉字占两个字节),当将其转换成ASCII码时,所占空间为6个字节BYTE。

 

 

总结:

 

使用这两个方法时,要注意求取转换成为char *后,其所占用的字节数

 

 

WideCharToMultiByte求解字节数方法:

 

 int iTexLen;

iTextLen=WideCharToMultiByte(CP_ACP,0,strText,-1,NULL,0,NULL,0);/确定strText中的CString转换为ASCII后,所需的字节数

 

 

 

USES_CONVERSION 求解字节数方法:

 

 USES_CONVERSION;

 

 

    LPSTR lpText=W2A(strText);     //  将strText字符串转换成ASCII char *类型,字符串以'/0'作为结束

    LPSTR  p=lpText;                      //   指针P用来计算char *字符串的长度
 int length;
 for ( length=0;;length++)           //    计算字符串的长度
 {
  if (*p=='/0')  break;
  p++;

 }

你可能感兴趣的:(File,null,byte)