c++字符编码转换

// MyTransferTCHAR.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<stdlib.h>


#include<Windows.h>
#include<tchar.h>    //include <string.h> vs <wchar.h>

/*part1:宽字符和C*/
//宽字符并不一定是UNICODE,UNICODE只是宽字符编码的一种体现
//c语言中宽字符是基于 wchar_t数据类型
//L"hello",L'A':表示应被解释为宽字符
//wchar_t a[]=L"kuanzihello";
/*
<string.h> :
size_t  __cdecl strlen(_In_z_ const char * _Str)
<wchar.h>:
size_t __cdecl wcslen(_In_z_ const wchar_t * _Str)
<tchar.h>
#define _tcslen wcslen/strlen

typedef wchar_t TCHAR
typedef char TCHAR
*/

/*part2:宽字符和Windows*/
//windows.h-->windef.h(winnt.h)
/*#include<winnt.h>
typedef char CHAR
typedef wchar_t TCHAR

*/


/*part3:Windows中字符串函数*/
/*lstrlen
lstrcpy
lstrcpyn
lstrcat
lstrcmp
lstrcmpi*/
//sprintf/swprintf-->_stprintf
//
//vsprintf:执行可变参数 va_list,va_start and va_end(#include<stdarg.h>)


void test_strlen()
{
 wchar_t *pw=L"hello!";
 int iLength=0;
 iLength=wcslen(pw);
}

void TCHAR2CHAR()
{
 TCHAR src[]=TEXT("hello!");
 char dest[256]={0};
 int length=0;  //7
 length=WideCharToMultiByte(CP_ACP,0,src,-1,NULL,0,NULL,NULL);
 WideCharToMultiByte(CP_ACP,0,src,-1,dest,length,NULL,NULL);
 printf("%s\n",dest);
}

void CHAR2TCHAR()
{
 CHAR src[]="hello!";
 TCHAR dest[256]={0};
 int length=0;  //7
 length=MultiByteToWideChar(CP_ACP,0,src,-1,NULL,0);
 MultiByteToWideChar(CP_ACP,0,src,-1,dest,length);
 printf("%s\n",dest);
}

int _tmain(int argc, _TCHAR* argv[])
{
 CHAR2TCHAR();
 return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值