WINCE下 CString 和Char * 的转换

最近项目中遇到关于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(); //一定要释放

posted @ 2009-10-01 20:52  Jade  阅读(512)  评论(0编辑  收藏  举报