Unicode编码下字符串转换

VC\MFC当中CString、string、char、char*、char数组、int等类型之间的转换令人晕头转向,特地搜集多篇文章资料,利用代码实例等清晰的理清他们之间的关系和如何转换,其实非常简单。

1、CString std::string互转
std::string strstring = "std::string";
CString strCString = _T("CString");

//CString->std::string
strstring = CStringA(strCString);

//std::string->CString
strCString = strstring.c_str();

2、int和CString
//CString->int
CString strInt = _T("123");
int n = 0;
n = _ttoi(strInt);
n = 9;
//int->CString
strInt.Format(_T("%d"), n);

3、double和CString
CString strdouble = _T("123.00");
double dVal = 0;
//CString->double
dVal = _tstof(strdouble);
dVal = 999.0;
//double->CString
strdouble.Format(_T("%f"), dVal);

//4、char*和CString
char* chchar = "chahdaslhdf";
CString strCh = _T("char");
//char*->CString
strCh = chchar;
//或者
//预转换,得到所需空间的大小
int wcsLen = ::MultiByteToWideChar(CP_ACP, NULL, chchar, strlen(chchar), NULL, 0);
//分配空间要给‘\0‘留个空间,MultiByteToWideChar不会给‘\0‘空间
wchar_t* wszString = new wchar_t[wcsLen + 1];
//转换
::MultiByteToWideChar(CP_ACP, NULL, chchar, strlen(chchar), wszString, wcsLen);
//最后加上‘\0‘
wszString[wcsLen] = ‘\0‘;
//附加到CString对象上
CString content;
content.Append(wszString);

//CString->char*
int len=strCh.GetLength();
char *buffer=new char[len+1];
memset(buffer,0,len+1);
WideCharToMultiByte(CP_OEMCP, NULL, (LPCWSTR)strCh, -1,NULL, 0, NULL, FALSE);
WideCharToMultiByte(CP_OEMCP, NULL, (LPCWSTR)strCh, -1,(LPSTR)buffer, len, NULL, FALSE);
buffer[len]=‘\0‘;
delete []buffer;

5、将wchar_t*转化为char*

char* UnicodeToAnsi(const wchar_t* szStr)
{
    int nLen = WideCharToMultiByte( CP_ACP, 0, szStr, -1, NULL, 0, NULL, NULL );
    if (nLen == 0)
    return NULL;

char* pResult = new char[nLen+1];
    WideCharToMultiByte( CP_ACP, 0, szStr, -1, pResult, nLen, NULL, NULL );
    return pResult;
}

6、将char*转化为wchar_t*

wchar_t* AnsiToUnicode(const char* szStr)
{
    int nLen = MultiByteToWideChar( CP_ACP, MB_PRECOMPOSED, szStr, -1, NULL, 0 );
    if (nLen == 0)
    return NULL;

wchar_t* pResult = new wchar_t[nLen+1];
    MultiByteToWideChar( CP_ACP, MB_PRECOMPOSED, szStr, -1, pResult, nLen );
    return pResult;
}

时间: 2024-08-29 04:47:51

Unicode编码下字符串转换的相关文章

字符编码(续)---Unicode与ANSI字符串转换以及分辨字符编码形式

Unicode与ANSI字符串转换 我们使用windows函数MultiByteToWideChar将多字节字符串转换为宽字符字符串,如下: int MultiByteToWideChar( UINT uCodePage, DWORD dwFlags, PCSTR pMultiByteStr, int cbMultiByte, PWSTR pWideCharStr, int cchWideChar); uCodePage参数标识了与多字节字符串关联的一个代码页值.dwFlags参数允许我们进行额

JavaScript中字符串与Unicode编码的互相转换

JavaScript中字符串与Unicode编码的互相转换 这段代码演示了JavaScript中字符串与Unicode编码的转换: // 为了控制台的演示方便, 变量没有添加 var 定义 // 实际编程中请避免 // 字符串 str = "中文"; // 获取字符 char0 = str.charAt(0); // "中" // 数字编码值 code = str.charCodeAt(0); // 20013 // 编码互转 str0 = String.fromC

MFC Unicode编码下,获取CString描述的IP地址并赋值到CIPAddressCtrl控件中

1. UniCode编码下,CString中字符被声明为宽字,应使用如下方法获取char* CString cstrIp = strIp; // Unicode 下将 CString转为char* int n = cstrIp.GetLength(); int len = WideCharToMultiByte(CP_ACP,0,cstrIp,cstrIp.GetLength(),NULL,0,NULL,NULL); char *p = new char[len+1]; WideCharToMu

ASCII,Utf8,Unicode编码下的中英文字符大小

一,测试Demo namespace 不同编码下的中英文字符大小 { class Program { static void Main(string[] args) { ShowCode(); } private static void ShowCode() { string[] strArray = { "b","abc","乙","甲乙丙丁"}; byte[] buffer; string mode, back; fore

Unicode编码解码在线转换工具

Unicode 是基于通用字符集(Universal Character Set)的标准来发展,并且同时也以书本的形式(The Unicode Standard,目前第五版由Addison-Wesley Professional出版,ISBN-10: 0321480910)对外发表. 2006年7月的最新版本的 Unicode 是5.0版本. 2005年3月31日推出的Unicode 4.1.0 .另外,5.0 Beta于2005年12月12日推出,5.2版本(unicode standard)

字符串在各种编码下的转换

char*   ——>   CStirng void CharToCString(CString& str, char *chr) { CString pWideChar=_T(""); // 计算char *数组大小,以字节为单位,一个汉字占两个字节 int charLen = strlen(chr); //计算多字节字符的大小,按字符计算. int len = MultiByteToWideChar(CP_ACP,0,chr,charLen,NULL,0); //为宽

MFC:在Unicode编码下CString、char *转换

一.CString转char * CString pb=_T("abc"); int len = WideCharToMultiByte(CP_UTF8, 0, pb, pb.GetLength(), NULL, 0, NULL, NULL); char* pa = new char[len + 1]; len = WideCharToMultiByte(CP_UTF8, 0, pb, pb.GetLength(), pa, len + 1, NULL, NULL); pa[len]

UNICODE编码下setclipbdata

搞了好久,要么setclipboarddata中断,要么只能复制第一个字母. 只能复制首字母是因为UNICODE下CString强制转换wchar_t*不行. setclipboarddata中断是因为wcscpy_s拷贝字符串到内存地址的rsize_t不对. 如下是OK的代码: if (OpenClipboard()){ CString temp; GetDlgItemText(IDC_EDIT1, temp); EmptyClipboard(); HGLOBAL hmem=NULL; hme

.Net(c#)汉字和Unicode编码互相转换

{"Tilte": "\u535a\u5ba2\u56ed", "Href": "http://www.cnblogs.com"} 经常遇到这样内容的json字符串,原来是把其中的汉字做了Unicode编码转换. Unicode编码: 将汉字进行UNICODE编码,如:“王”编码后就成了“\王”,UNICODE字符以\u开始,后面有4个数字或者字母,所有字符都是16进制的数字,每两位表示的256以内的一个数字.而一个汉字是由两