MFC多节字符与宽字符相互转换

2012-02-11 19:20:34|?次阅读|上传:wustguangh【已有?条评论】发表评论

关键词:C/C++, 字符处理|来源:唯设编程网

VC6.0里面默认的字符是多节字符,在VS2002以后增加了Unicode字符集的支持,在VC实际编程过程中,需要经常进行多字节宽字符相互转换,使用window.h提供的转换函数可以方便的实现宽字符和多节字符相互转换,网上搜集整理了一下,以供日后参考。 

1.多节字符集转成宽字符

主要使用MultiByteToWideChar函数实现,实例如下:

string str;
 
cout<<"please input chinese"<<endl;
cin>>str;
//setlocale(LC_CTYPE,".chs");
const char* cstr1 = str.c_str();
//int len = 2*sizeof(cstr);
 
int WLen = MultiByteToWideChar(CP_ACP,   
                                // code page
                               0,        
                               // character-type options
                               cstr1,   
                                // string to map
                               -1,      
                                // number of bytes in string
                               NULL,     
                               // wide-character buffer
                               0        
                                // size of buffer
                               );
wchar_t * buffer = new wchar_t[WLen+1];
int RetWLen = MultiByteToWideChar(CP_ACP,  
                                  // code page
                                  0,       
                                  // character-type options
                                  cstr1,   
                                  // string to map
                                  -1,      
                                  // number of bytes in string
                                  buffer,  
                                  // wide-character buffer
                                  WLen     
                                  // size of buffer
                                  );

buffer[WLen] = '/0';
printf("/nthe translated num is %d",RetWLen);

2.宽字符转多字节

主要使用WideCharToMultiByte函数实现,实例如下:

 //这里第六个参数若为表示返回宽字符对应多字节的长度
int MLen = WideCharToMultiByte(
                               CP_ACP,            
                               // code page
                               0,           
                                // performance and mapping flags
                               buffer,   
                                // wide-character string
                               -1,          
                               // number of chars in string
                               NULL,     
                               // buffer for new string
                               0,          
                               // size of buffer
                               NULL,    
                                // default for unmappable chars
                               NULL  
                               // set when default char used
                               );
                              
char* MultiBte = new char[MLen+1];
//完成转换
int RetMLen = WideCharToMultiByte(
                                  CP_ACP,            
                                  // code page
                                  0,            
                                  // performance and mapping flags
                                  buffer,    
                                  // wide-character string
                                  -1,          
                                  // number of chars in string
                                  MultiBte,     
                                  // buffer for new string
                                  MLen,          
                                  // size of buffer
                                  NULL,     
                                  // default for unmappable chars
                                  NULL  
                                  // set when default char used
                                  );

printf("/nthe translated MultiByte num is :%d",RetMLen);
printf("/nthe Multibyte value:");
MultiBte[MLen]='/x0';
fputs(MultiBte,stdout); //可直接在终端输出

 需要导入头文件 windows.h,以上程序才能正常编译。
 

发表评论0条 】
网友评论(共?条评论)..
MFC多节字符与宽字符相互转换