0
点赞
收藏
分享

微信扫一扫

ObjectArxC++宽字节与单字节互转

ObjectArxC++宽字节与单字节互转

在ObjectArxC++编程中,默认使用wchart_t类型字符。wchart_t使用了多种别名,举例如下:

typedef wchar_t WCHAR;    // wc,   16-bit UNICODE character

typedef WCHAR *PWCHAR, *LPWCH, *PWCH;
typedef CONST WCHAR *LPCWCH, *PCWCH;

typedef __nullterminated WCHAR *NWPSTR, *LPWSTR, *PWSTR;
typedef __nullterminated PWSTR *PZPWSTR;
typedef __nullterminated CONST PWSTR *PCZPWSTR;
typedef __nullterminated WCHAR UNALIGNED *LPUWSTR, *PUWSTR;
typedef __nullterminated CONST WCHAR *LPCWSTR, *PCWSTR;
typedef __nullterminated PCWSTR *PZPCWSTR;
typedef __nullterminated CONST WCHAR UNALIGNED *LPCUWSTR, *PCUWSTR;

typedef __nullnullterminated WCHAR *PZZWSTR;
typedef __nullnullterminated CONST WCHAR *PCZZWSTR;
typedef __nullnullterminated WCHAR UNALIGNED *PUZZWSTR;
typedef __nullnullterminated CONST WCHAR UNALIGNED *PCUZZWSTR;

typedef __possibly_notnullterminated WCHAR *PNZWCH;
typedef __possibly_notnullterminated CONST WCHAR *PCNZWCH;
typedef __possibly_notnullterminated WCHAR UNALIGNED *PUNZWCH;
typedef __possibly_notnullterminated CONST WCHAR UNALIGNED *PCUNZWCH;

涉及到了宽字节与单字节转换的方式,主要包含如下:

  1. LPCWSTR转为std::string,std:string转为LPCWSTR。
  2. wchar_t转为char_t,char_t转为wchar_t。
  3. wchar_t转为double,double转为wchar_t。
  4. wchar宽字节转char单字节,char单字节转wchar宽字节。
  5. utf8转unicode,unicode转utf8。

代码示例如下:

1.LPCWSTR转为std::string:

//zhaoanan lpcwstr转String
/************************************************************************/
/*-------------------------lpcwstrToString------------------------------------------*/
/*****日期:2022-10-17**************************************************/
/*****作者:zhaoanan****************************************************/
/*****函数功能:函数******************************************/
/*****输入参数:********************************************************/
/**********1.from_str-输入字符串**********************************/
/**********2.to_str-返回字符串**********************************/
/*****输出参数:void**********************************************************************/
/*****返回值: 无*******************************************************************/
/************************************************************************/
void AttributeUtil::lpcwstrToString(LPCWSTR from_str, string &to_str)
{
try
{
string re_str = "";
int nLen = WideCharToMultiByte(CP_ACP, 0, from_str, -1, NULL, 0, NULL, NULL);
if (nLen <= 0)
re_str = "";

char* pszDst = new char[nLen];
if (NULL == pszDst)
re_str = "";

WideCharToMultiByte(CP_ACP, 0, from_str, -1, pszDst, nLen, NULL, NULL);
pszDst[nLen - 1] = 0;

std::string strTmp(pszDst);
delete[] pszDst;

re_str = strTmp;
to_str = re_str;
}
catch (CMemoryException* e)
{
AfxMessageBox(_T("CMemoryException in lpcwstrToString")) ;
}
catch (CFileException* e)
{
AfxMessageBox(_T("CFileException in lpcwstrToString")) ;
}
catch (CException* e)
{
AfxMessageBox(_T("CException in lpcwstrToString")) ;
}
catch (_com_error& e)
{
CString sBuff = CBlkUtility::GetErrorDescription(e) ;
AfxMessageBox(sBuff) ;
}
catch (...)
{
AfxMessageBox(_T("unknown CException in lpcwstrToString")) ;
}
}

2.stringToLPCWSTR:

//zhaoanan string转LPCWSTR
/************************************************************************/
/*-------------------------stringToLPCWSTR------------------------------------------*/
/*****日期:2022-10-17**************************************************/
/*****作者:zhaoanan****************************************************/
/*****函数功能:公里标截取函数******************************************/
/*****输入参数:********************************************************/
/**********1.from_str-输入字符串**********************************/
/**********2.to_str-返回字符串**********************************/
/*****输出参数:void**********************************************************************/
/*****返回值: 无*******************************************************************/
/************************************************************************/
void AttributeUtil::stringToLPCWSTR(string from_str, LPCWSTR &to_str)
{
try
{
size_t origsize = from_str.length() + 1;
const size_t newsize = 100;
size_t convertedChars = 0;
wchar_t *wcstring = (wchar_t *)malloc(sizeof(wchar_t)*(from_str.length()-1));
mbstowcs_s(&convertedChars, wcstring, origsize, from_str.c_str(), _TRUNCATE);

to_str = wcstring;
}
catch (CMemoryException* e)
{
AfxMessageBox(_T("CMemoryException in stringToLPCWSTR")) ;
}
catch (CFileException* e)
{
AfxMessageBox(_T("CFileException in stringToLPCWSTR")) ;
}
catch (CException* e)
{
AfxMessageBox(_T("CException in stringToLPCWSTR")) ;
}
catch (_com_error& e)
{
CString sBuff = CBlkUtility::GetErrorDescription(e) ;
AfxMessageBox(sBuff) ;
}
catch (...)
{
AfxMessageBox(_T("unknown CException in stringToLPCWSTR")) ;
}
}

3.double与wchar互转如下:

// CString2double
/*-------------------------CString2double------------------------------------------*/
/*****日期:2021-6-10**************************************************/
/*****作者:zhaoanan****************************************************/
/*****输入参数:********************************************************/
/**********1.str-输入字符串**********************************/
/*****输出参数:void**********************************************************************/
/*****返回值: 无*******************************************************************/
/************************************************************************/
double CthEntity::CString2double(CString str)
{
try
{
//注意:以下n和len的值大小不同,n是按字符计算的,len是按字节计算的
int n = str.GetLength(); // n = 14, len = 18

//获取宽字节字符的大小,大小是按字节计算的
int len = WideCharToMultiByte(CP_ACP,0,str,str.GetLength(),NULL,0,NULL,NULL);

//为多字节字符数组申请空间,数组大小为按字节计算的宽字节字节大小
char * pFileName = new char[len+1]; //以字节为单位

//宽字节编码转换成多字节编码
WideCharToMultiByte(CP_ACP,0,str,str.GetLength(),pFileName,len,NULL,NULL);

pFileName[len+1] = '\0'; //多字节字符以'\0'结束

double num=atof(pFileName);
delete []pFileName;
return num;
}
catch (...) {
CString strMsg;
//acutPrintf(_T("请检查配置文件%s是否存在 \n"),strFileName);
strMsg.Format(_T("\n UnKnown Exception in FileName: %s\t,Function=%s\t,Line=%d"), __FILE__,__FUNCTION__,__LINE__);
AfxMessageBox(strMsg);
}
}
// DoubleToString
/*-------------------------DoubleToString------------------------------------------*/
/*****日期:2021-6-10**************************************************/
/*****作者:zhaoanan****************************************************/
/*****输入参数:********************************************************/
/**********1.value-输入字符串**********************************/
/*****输出参数:void**********************************************************************/
/*****返回值: 无*******************************************************************/
/************************************************************************/
CString CthEntity::DoubleToString( double value, int precision = 2 )
{
CString strFormat, str;
//%06.2f,表示至少输出为六位包括小数点,不足的在前面补0
strFormat.Format(TEXT("%%06.%df"), precision);
str.Format(strFormat, value);

return str;
}

4.unicode与ansi互转如下:

// UnicodeToUtf8
/*-------------------------UnicodeToUtf8------------------------------------------*/
/*****日期:2021-6-10**************************************************/
/*****作者:zhaoanan****************************************************/
/*****输入参数:********************************************************/
/**********1.strUnicode-输入字符串**********************************/
/*****输出参数:void**********************************************************************/
/*****返回值: 无*******************************************************************/
/************************************************************************/
string CBlkUtility::UnicodeToUtf8(CString strUnicode)
{
wchar_t* unicode = strUnicode.AllocSysString();
int len;
len = WideCharToMultiByte(CP_UTF8, 0, unicode, -1, NULL, 0, NULL, NULL);
char *szUtf8 = (char*)malloc(len + 1);
memset(szUtf8, 0, len + 1);
WideCharToMultiByte(CP_UTF8, 0, unicode, -1, szUtf8, len, NULL, NULL);
string result = szUtf8;
free(szUtf8);
return result;
}
// Utf8ToUnicode
/*-------------------------Utf8ToUnicode------------------------------------------*/
/*****日期:2021-6-10**************************************************/
/*****作者:zhaoanan****************************************************/
/*****输入参数:********************************************************/
/**********1.strUtf8-输入字符串**********************************/
/*****输出参数:void**********************************************************************/
/*****返回值: 无*******************************************************************/
/************************************************************************/
CString CBlkUtility::Utf8ToUnicode(string strUtf8)
{
/* 预转换,得到所需空间的大小 */
int nLen = ::MultiByteToWideChar( CP_UTF8, NULL, strUtf8.data(), strUtf8.size(), NULL, 0 );
/* 转换为Unicode */
std::wstring wbuffer;
wbuffer.resize( nLen );
::MultiByteToWideChar( CP_UTF8, NULL, strUtf8.data(), strUtf8.size(), (LPWSTR) (wbuffer.data() ), wbuffer.length() );

#ifdef UNICODE
return(CString( wbuffer.data(), wbuffer.length() ) );
#else
/*转换为ANSI,得到转换后长度*/
nLen = WideCharToMultiByte( CP_ACP, 0, wbuffer.data(), wbuffer.length(), NULL, 0, NULL, NULL );
string ansistr;
ansistr.resize( nLen );

/* 把unicode转成ansi */
WideCharToMultiByte( CP_ACP, 0, (LPWSTR) (wbuffer.data() ), wbuffer.length(), (LPSTR) (ansistr.data() ), ansistr.size(), NULL, NULL );
return(CString( ansistr.data(), ansistr.length() ) );
#endif
}

5.参考意见和网页地址:

  1. ​​https://blog.csdn.net/a137748099/article/details/111051611​​
  2. ​​http://t.zoukankan.com/aicro-p-1492770.html​​
举报

相关推荐

0 条评论