字符串: 怎样在ANSI 和 UNICODE间做转换
2005-07-18 13:41
369 查看
问题: 怎样在ANSI 和 UNICODE间做转换?
答案:
这个答案启发自 Yves M贴出的一张回复(reply)
ANSI 到 UNICODE:
这个转换用函数MultiByteToWideChar()完成
代码:
--------------------------------------------------------------------------------
char *ansistr = "Hello";
int a = lstrlenA(ansistr)+1;
BSTR unicodestr = SysAllocStringLen(NULL, 2*a);
MultiByteToWideChar(CP_ACP, 0, ansistr, a, unicodestr, a);
AfxMessageBox(CString(unicodestr), MB_OK, 0); //displays "Hello"
//... when done, free the BSTR
SysFreeString(unicodestr);
--------------------------------------------------------------------------------
UNICODE 到 ANSI:
UNICODE大多数情况下被OLE函数返回, 像这个
代码:
--------------------------------------------------------------------------------
HRESULT SomeOLEFunction(BSTR &bstr)
{
bstr = ::SysAllocString(L"Hello");
return S_OK;
}
--------------------------------------------------------------------------------
这个转换用WideCharToMultiByte()函数完成
代码:
--------------------------------------------------------------------------------
BSTR unicodestr;
SomeOLEFunction(unicodestr);
int a = SysStringLen(unicodestr)+1;
char *ansistr = new char[a];
WideCharToMultiByte(CP_ACP,
0,
unicodestr,
-1,
ansistr,
a,
NULL,
NULL);
AfxMessageBox(ansistr, MB_OK, 0); // will display "Hello"
//...use the strings, then free their memory:
delete[] ansistr;
SysFreeString(unicodestr);
--------------------------------------------------------------------------------
答案:
这个答案启发自 Yves M贴出的一张回复(reply)
ANSI 到 UNICODE:
这个转换用函数MultiByteToWideChar()完成
代码:
--------------------------------------------------------------------------------
char *ansistr = "Hello";
int a = lstrlenA(ansistr)+1;
BSTR unicodestr = SysAllocStringLen(NULL, 2*a);
MultiByteToWideChar(CP_ACP, 0, ansistr, a, unicodestr, a);
AfxMessageBox(CString(unicodestr), MB_OK, 0); //displays "Hello"
//... when done, free the BSTR
SysFreeString(unicodestr);
--------------------------------------------------------------------------------
UNICODE 到 ANSI:
UNICODE大多数情况下被OLE函数返回, 像这个
代码:
--------------------------------------------------------------------------------
HRESULT SomeOLEFunction(BSTR &bstr)
{
bstr = ::SysAllocString(L"Hello");
return S_OK;
}
--------------------------------------------------------------------------------
这个转换用WideCharToMultiByte()函数完成
代码:
--------------------------------------------------------------------------------
BSTR unicodestr;
SomeOLEFunction(unicodestr);
int a = SysStringLen(unicodestr)+1;
char *ansistr = new char[a];
WideCharToMultiByte(CP_ACP,
0,
unicodestr,
-1,
ansistr,
a,
NULL,
NULL);
AfxMessageBox(ansistr, MB_OK, 0); // will display "Hello"
//...use the strings, then free their memory:
delete[] ansistr;
SysFreeString(unicodestr);
--------------------------------------------------------------------------------
相关文章推荐
- WinCe和Windows Mobile下的Unicode和ANSI字符串转换的方法
- Ansi、Unicode、UTF8字符串之间的转换和写入文本文件
- 移植性问题のCString类型转换,字符串操作和UNICODE、ANSI字符集
- Ansi、Unicode、UTF8等编码字符串之间的转换和写入文本文件(学习中……)
- 如何在Unicode与ANSI之间转换字符串
- UNICODE和ANSI字符串的转换
- 字符串处理 - ANSI - Unicode - UTF8 转换
- Ansi、Unicode、UTF8字符串之间的转换和写入文本文件
- Ansi、Unicode、UTF8字符串之间的转换和写入文本文件
- Unicode 和Ansi下的字符和字符串转换问题
- Ansi、Unicode、UTF8字符串之间的转换和写入文本文件
- [C语言]字符串处理 - ANSI - Unicode - UTF8 转换
- Unicode 与 ANSI 字符串转换
- Ansi、Unicode、UTF8字符串之间的转换和写入文本文件
- Ansi、Unicode、UTF8等编码字符串之间的转换和写入文本文件(学习中……)
- VC中Ansi、Unicode、UTF8字符串之间的转换和写入文本
- Ansi、Unicode、UTF8字符串之间的转换和写入文本文件
- 字符串处理 - ANSI - Unicode - UTF8 转换
- VC中Ansi、Unicode、UTF8字符串之间的转换和写入文本
- Ansi、Unicode、UTF8字符串之间的转换和写入文本文件