/ 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;
}