最近项目中遇到关于CString和Char * 相互转换及存储问题,网上有很多描述方法,但是不太适合Wince下编程使用,究其根本原因有不外乎两点:一是wince自身的限制,二是由于WinCE的本地文件格式采用了Unicode编码。
1. CString转换到Char *
CString Currentfilename = “hello.txt”;
char str[128];
int i;
int length;
length = CurrentFilename.GetLength();
memset(str, 0, 128);
i= WideCharToMultiByte(CP_ACP, WC_COMPOSITECHECK|WC_DEFAULTCHAR,CurrentFilename.GetBuffer(length),length+1, str,128,0,0);

2. Char *转换到CString
char *LastOpenFile;
CString Currentfilename;
int widecharlen;

widecharlen = MultiByteToWideChar(CP_ACP, MB_COMPOSITE, LastOpenFile, -1, 0, 0); //计算从Ansi转换到Unicode后需要的字节数
CurrentFilename.GetBuffer(widecharlen); //为转换后保存Unicode字符串分配内存
MultiByteToWideChar(CP_ACP, MB_COMPOSITE, LastOpenFile, -1, CurrentFilename.GetBuffer(widecharlen), widecharlen); //从Ansi转换到Unicode字符
CurrentFilename.ReleaseBuffer(); //一定要释放

相关文章:

  • 2022-01-06
  • 2022-12-23
  • 2021-07-12
  • 2022-01-20
  • 2021-09-21
  • 2021-08-02
  • 2022-12-23
  • 2021-09-16
猜你喜欢
  • 2021-08-08
  • 2021-09-01
  • 2021-09-08
相关资源
相似解决方案