VC .NET中String^ 与ANSI C char*互转
来源:互联网 发布:华莱士油炸蛤蟆知乎 编辑:程序博客网 时间:2024/06/17 02:38
VC.NET中的String类是利用Unicode字符集编码来表示文本。Unicode字符集中每个字符(汉字、英文字母)都占2个字节,且其字符串是以2个连续的/0结尾的。
ANSI的ASCII字符集是最常见的字符集,常用于表示txt的文本文件。在ASCII字符集中英文占一个字节,汉字2个字节,且其字符串是以一个/0结尾的。
在利用VC.NET进行混合编程时,经常需要实现String与char*的互转,例如在TextBox控件中输入的Text作为fopen或者CreateFile的文件名参数时,常需要实现char*的转化。故提供如下的转化方法:
#include "stdafx.h"
#include <stdio.h>
#include <windows.h>
int main(array<System::String ^> ^args)
{
// String^ converts to char*
char szDestOutChar[256];
String^ SrcInStr = "C://Documents and Settings//Adiminstrator//我的文档//swq.txt";
int SourceStrLen = SrcInStr->Length;
wchar_t *pwszStr = new wchar_t[SourceStrLen + 1];
array <System::Char>^ pClr = SrcInStr->ToCharArray();
for (int i = 0; i < SourceStrLen; i++)
{
pwszStr[i] = pClr[i];
}
pwszStr[SourceStrLen] = '/0';
::WideCharToMultiByte(CP_ACP, NULL, pwszStr, -1, szDestOutChar, 256, NULL, NULL);
// char* converts to String^
char* szInSrcChar = "C://Documents and Settings//Adiminstrator//我的文档//swq.txt";
int wcsLen = ::MultiByteToWideChar(CP_ACP, NULL, szInSrcChar, strlen(szInSrcChar), NULL, 0);
wchar_t* wszStr = new wchar_t[wcsLen + 1];
::MultiByteToWideChar(CP_ACP, NULL, szInSrcChar, strlen(szInSrcChar), wszStr, wcsLen);
wszStr[wcsLen] = '/0';
String^ ClrOutStr = gcnew String(wszStr);
System::Console::WriteLine("Press ENTER key to exit !");
System::Console::Read();
delete pwszStr;
delete wszStr;
return 0;
}
- VC .NET中String^ 与ANSI C char*互转
- 在C++.NET中,System::String^与char*相互转换
- C++string与char*
- VS中CString与char *、string互转
- C++ string 与 C char *
- c/c++中string与char的区别
- VC中char* 与 char[]的区别
- VC中char* 与 char[]的区别
- char* 与string 互转
- string 与 char* 互转
- VC中CSTRING 转char*,,,转string
- ANSI与UTF-8编码互相转换,CString,std::string,char*,const char*互相转换
- Visual C++ .NET 中 System::String^ 和 char * 互转
- Visual C++ .NET 中 System::String^ 和 char * 互转
- C/C++中string与char *和 char []之间的转换
- 【C\C++】char[] 与 string 的转换
- VC.net 2005 中CString转成char *
- VC 中 Cstring 与 int、float、double、char、string数据类型的相互转换
- 查看电脑配置命令_注册表
- 多目标最优化简介
- oracle中遍历树形结构的方法
- 态度
- Porting a Sample Radio Interface Layer (RIL) to a New Platform
- VC .NET中String^ 与ANSI C char*互转
- List按指定字段排序排序
- Harmonic number sum
- ril驱动解析
- JAVA反射机制入门(一)--Class类API实例介绍
- 谈垂直搜索的未来
- IT的臭皮匠!
- struts + hibernate + spring 整体的认识
- 读不起,同学,我们学校不准备在西华师范大学招收本科生教师