/ c_plus_plus_1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
#include <utility>
#include <map>
#include <memory>
#include <Windows.h>
#include <assert.h>
#include <stdio.h>
#include "ye.h"
#include "windows.h"
#include "tchar.h "
#include "wchar.h "
#include <locale.h>
#include <stdio.h>
#include <wchar.h>
#pragma once
class chAWTrans
{
public:
chAWTrans(const char* szAnsi);
chAWTrans(const wchar_t* wszString);
~chAWTrans(void);
operator char* ();
operator wchar_t* ();
private:
char* m_szAnsi;
wchar_t* m_wszString;
};
#define STR_MIN(a,b) (((a) < (b)) ? (a) : (b))
inline void *mlmemcpy(void* dst,int dstSize,const void * src,int srcSize)
{
return memcpy(dst,src,STR_MIN(dstSize,srcSize));
}
inline int mlstrcpyA(char *dst,int nCharDst,const char *src)
{
int nActual = STR_MIN((int)strlen(src),nCharDst-1);
if (nActual >= 0)
{
memcpy(dst,src,nActual * sizeof(char));
dst[nActual] = '\0';
return nActual;
}
else
return 0;
}
inline int mlstrcpyW(wchar_t *dst,int nCharDst,const wchar_t *src)
{
int nActual = STR_MIN((int)wcslen(src),nCharDst-1);
if (nActual >=0)
{
memcpy(dst,src,nActual*sizeof(wchar_t));
dst[nActual] = '\0';
return nActual;
}
else
return 0;
}
wchar_t* Ansi2Unicode(const char* szAnsi)
{
// 预转换,得到所需空间的大小
int wcsLen = ::MultiByteToWideChar(CP_ACP, NULL, szAnsi, (int)strlen(szAnsi), NULL, 0);
// 分配空间要给'/0'留个空间,MultiByteToWideChar不会给'/0'空间
wchar_t* wszString = new wchar_t[wcsLen+1];
::MultiByteToWideChar(CP_ACP, NULL, szAnsi, (int)strlen(szAnsi), wszString, wcsLen);
// 最后加上结束符'/0'
wszString[wcsLen] = L'\0';
return wszString;
}
char* Unicode2Ansi(const wchar_t* wszString)
{
int ansiLen = ::WideCharToMultiByte(CP_ACP, NULL, wszString, wcslen(wszString), NULL, 0, NULL, NULL);
char* szAnsi = new char[ansiLen + 1];
// Unicode版对应的strlen是wcslen
::WideCharToMultiByte(CP_ACP, NULL, wszString, wcslen(wszString), szAnsi, ansiLen, NULL, NULL);
// 最后加上'/0'
szAnsi[ansiLen] = '\0';
return szAnsi;
}
chAWTrans::chAWTrans(const char* szAnsi) : m_szAnsi(NULL)
{
m_wszString = Ansi2Unicode(szAnsi);
}
chAWTrans::chAWTrans(const wchar_t* wszString) : m_wszString(NULL)
{
m_szAnsi = Unicode2Ansi(wszString);
}
chAWTrans::~chAWTrans(void)
{
}
chAWTrans::operator char* ()
{
if (!m_szAnsi && m_wszString)
{
m_szAnsi = Unicode2Ansi(m_wszString);
}
return m_szAnsi;
}
chAWTrans::operator wchar_t* ()
{
if (!m_wszString && m_szAnsi)
{
m_wszString = Ansi2Unicode(m_szAnsi);
}
return m_wszString;
}
int _tmain(int argc, _TCHAR* argv[])
{
char p1[] = "yeguangming nihao ";
char p2[] = "liliqin is my wife ";
char p3[128];
wchar_t p4[128];
wchar_t *p5 = Ansi2Unicode("yeguangming is best");//= "yeguangming is best";
//char *p =(char*) mlmemcpy(p1,(int)strlen(p1),p2,(int)strlen(p2));
//printf("%s \n",p);
int iCount = mlstrcpyA(p3,128,p2);
printf("p3 = %s,\np2 = %s,iCount = %d\n",p3,p2,iCount);
iCount = mlstrcpyW(p4,wcslen(p4),p5);
printf(" p4 = %s,\n p5 = %s,iCount = %d\n",p4,p5,iCount);
wchar_t *wc;
wchar_t *ws = L"中文Chinese";
setlocale(LC_ALL, "");
wchar_t *szwOne= L"program 叶广明yeguangming";
wprintf(L"%ls\n\n",szwOne);
wprintf(L"%ls\n\n", ws);
for(wc = ws; *wc != 0; ++wc){
wprintf(L"%lc", *wc);
}
printf("\n");
return 0;
}
相关文章
- 重操JS旧业第二弹:数据类型与类型转换
- javascript基础知识梳理-Number与String之间的互相转换【转】
- xsd与xml和类(class)对象之间的互相转换
- java开发系统内核:应用程序与内核运行环境的交叉转换
- Go字节数组与字符串相互转换
- 应用程序开发——集合(泛型、List与Set集合互转换、Map、Properties、集合工具类Collections、同步控制)
- 将string转换成char* (转)
- java byte数组与String的相互转换
- MFC中如何将16进制字符转化成10进制,包括CString与char*的转换
- ArcGIS AddIN 10.3项目与10.1项目的相互转换