char和achar互转

#pragma once
#include "stdafx.h"

#ifndef _Convert_H_
#define _Convert_H_

//定义转换类
class Convert
{
public:
//定义一般函数
//------------------------------------------
ACHAR* ConvertCharPtrToAcharPtr(const char* src);
ACHAR* ConvertCharPtrToAcharPtr2(const char* src);
//
char* ConvertAcharPtrToCharPtr(const ACHAR* src);
char* ConvertAcharPtrToCharPtr2(const ACHAR* src);
//通用转换函数
_bstr_t Get_bstr_t(char* src);
_bstr_t Get_bstr_t_W(wchar_t* src);
char* GetCharPtr(_bstr_t bstrt);
wchar_t* GetWchar_t(_bstr_t bstrt);
ACHAR* GetAcharPtr(_bstr_t bstrt);

//CComBSTR GetCComBSTR(char* src);
};
#endif

 

#pragma once
#include "stdafx.h"
#include "Convert.h"

//----------------------------------------------------
//实现类成员函数
//----------------------------------------------------
ACHAR* Convert::ConvertCharPtrToAcharPtr(const char* src)
{
ACHAR* tmp;
_bstr_t AStr = src;
LPWSTR AstrW = LPTSTR(AStr);
tmp=(ACHAR *)AstrW;
return tmp;
}
ACHAR* Convert::ConvertCharPtrToAcharPtr2(const char* src)
{
// Convert to a wchar_t*
size_t srcsize = strlen(src) + 1;
size_t newsize = srcsize;
size_t convertedChars = 0;
wchar_t *wcstring;
wcstring=new wchar_t[newsize];
mbstowcs_s(&convertedChars, wcstring, srcsize, src, _TRUNCATE);
//wcscat_s(wcstring, L" (wchar_t *)");
//wcout << wcstring << endl;
return wcstring;
}
char* Convert::ConvertAcharPtrToCharPtr(const ACHAR* src) //
{
char* tmp;
_bstr_t bstrt(src);
tmp=GetCharPtr(bstrt);
return tmp;
}
char* Convert::ConvertAcharPtrToCharPtr2(const ACHAR* src)
{
// Convert to a char*
size_t srcsize = wcslen(src) + 1;
size_t newsize = srcsize;
size_t convertedChars = 0;
char *nstring;
nstring=new char[newsize];
wcstombs_s(&convertedChars, nstring, srcsize, src, _TRUNCATE);
return nstring;
}
//
_bstr_t Convert::Get_bstr_t(char* src)
{
_bstr_t bstrt(src);
return bstrt;
}
_bstr_t Convert::Get_bstr_t_W(wchar_t* src)
{
// Convert to a _bstr_t
_bstr_t bstrt(src);
//bstrt += " (_bstr_t)";
//cout << bstrt << endl;
return bstrt;
}
char* Convert::GetCharPtr(_bstr_t bstrt)
{
// Convert to a char*
size_t newsize = bstrt.length()+1;
char *nstring;nstring=new char[newsize];
strcpy_s(nstring,newsize,(char *)bstrt);
//strcat_s(nstring, " (char *)");
//cout << nstring << endl;
return nstring;
}
wchar_t* Convert::GetWchar_t(_bstr_t bstrt)
{
// Convert to a wchar_t*
int srcsize=bstrt.length()+1;
wchar_t *wcstring;wcstring=new wchar_t[srcsize];
wcscpy_s(wcstring,srcsize,(wchar_t *)bstrt);
//wcscat_s(wcstring, L" (wchar_t *)");
//wcout << wcstring << endl;
return wcstring;
}
ACHAR* Convert::GetAcharPtr(_bstr_t bstrt)
{
return GetWchar_t(bstrt);
}



//CComBSTR Convert::GetCComBSTR(char* src)
//{
// // Convert to a CComBSTR
// CComBSTR ccombstr(src);
// /*if (ccombstr.Append(L" (CComBSTR)") == S_OK)
// {
// CW2A printstr(ccombstr);
// cout << printstr << endl;
// }*/
// return ccombstr;
//}

posted @ 2019-08-05 09:44  久龄  阅读(676)  评论(0编辑  收藏  举报