一.string 与Cstring互转问题
1.string转CString (宽字符方式UNICODE)
(1).CA2T 宏,这种方法不安全
string s; CA2T szr(s.c_str()); CString = (LPCTSTR)szr;//(LPCTSTR)为强制类型转换
(2).MultiByteToWideChar函数
std::wstring WStringToWString(const std::string& str) { int size_str = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, NULL, 0); wchar_t *wide = new wchar_t[size_str]; MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, wide, size_str); std::wstring return_wstring(wide); delete[] wide; wide = NULL; return return_wstring; } string strx wstring x = WStringToWString(strx); cstring m_strReceive=m_strReceive.Format(_T("%s"), x.c_str());
(3).C语言提供的宽字符(wchar_t类型)和多字节字符( 一个字符用一个或者多个字节表示) 转换函数
std::wstring StringToWString(const std::string& str) { setlocale(LC_ALL, "chs"); const char* point_to_source = str.c_str(); size_t new_size = str.size() + 1; wchar_t *point_to_destination = new wchar_t[new_size]; wmemset(point_to_destination, 0, new_size); mbstowcs(point_to_destination, point_to_source, new_size); std::wstring result = point_to_destination; delete[]point_to_destination; setlocale(LC_ALL, "C"); return result; } string strx wstring x = StringToWString(strx); cstring m_strReceive=m_strReceive.Format(_T("%s"), x.c_str());
举例:
strcpy(sBuf, "我最棒"); size_t sSize=strlen(sBuf); wchar_t * dBuf=NULL; <SPAN style="COLOR: #ff0000">//注意:需要多分配一个空间,以存放终止符</SPAN> int dSize=mbstowcs(dBuf, sBuf, 0)+1; dBuf=new wchar_t[dSize]; wmemset(dBuf, 0, dSize); int nRet=mbstowcs(dBuf, sBuf, sSize); if(nRet<=0) { printf("转换失败\n"); } else { printf("转换成功%d字符\n", nRet); wprintf(L"%ls\n", dBuf); }
2.cstring 转 string(宽字符方式UNICODE)
vs2010 Unicode下:
CStringW str(L"test"); CStringA stra(str.GetBuffer(0)); str.ReleaseBuffer(); std::string strs (stra.GetBuffer(0)); stra.ReleaseBuffer();
3.WCHAR CHAR与string wstring的转换
(1)、wchar_t 与wstring
可以直接转换
wchar_t *x; wstirng y; x=y;
(2)、// wchar_t to string
void Wchar_tToString(std::string& szDst, wchar_t *wchar) { wchar_t * wText = wchar; DWORD dwNum = WideCharToMultiByte(CP_OEMCP,NULL,wText,-1,NULL,0,NULL,FALSE);// WideCharToMultiByte的运用 char *psText; // psText为char*的临时数组,作为赋值给std::string的中间变量 psText = new char[dwNum]; WideCharToMultiByte (CP_OEMCP,NULL,wText,-1,psText,dwNum,NULL,FALSE);// WideCharToMultiByte的再次运用 szDst = psText;// std::string赋值 delete []psText;// psText的清除 }
(3)、// string to wstring
void StringToWstring(std::wstring& szDst, std::string str) { std::string temp = str; int len=MultiByteToWideChar(CP_ACP, 0, (LPCSTR)temp.c_str(), -1, NULL,0); wchar_t * wszUtf8 = new wchar_t[len+1]; memset(wszUtf8, 0, len * 2 + 2); MultiByteToWideChar(CP_ACP, 0, (LPCSTR)temp.c_str(), -1, (LPWSTR)wszUtf8, len); szDst = wszUtf8; std::wstring r = wszUtf8; delete[] wszUtf8; }
二、BYTE 与16进制转换
1、BYTE转16进制
string * Byte2Hex(unsigned bArray[], int bArray_len) { string *strHex = new string(); int nIndex = 0; for (int i = 0; i<bArray_len; i++) { int high = bArray[0] / 16, low = bArray[0] % 16; strHex[nIndex] = (high<10) ? ('0' + high) : ('A' + high - 10); strHex[nIndex + 1] = (low<10) ? ('0' + low) : ('A' + low - 10); nIndex += 2; } return strHex; }
string bytestohexstring(char* bytes, int bytelength) { string str(""); string str2("0123456789abcdef"); for (int i = 0; i<bytelength; i++) { int b; b = 0x0f & (bytes[i] >> 4); char s1 = str2.at(b); str.append(1, str2.at(b)); b = 0x0f & bytes[i]; str.append(1, str2.at(b)); char s2 = str2.at(b); } return str; }
2、16进制转byte
(1)、
int Change(char s[], char bits[]) { int i, n = 0; for (i = 0; s[i]; i += 2) { if (s[i] >= 'A' && s[i] <= 'F') bits[n] = s[i] - 'A' + 10; else bits[n] = s[i] - '0'; if (s[i + 1] >= 'A' && s[i + 1] <= 'F') bits[n] = (bits[n] << 4) | (s[i + 1] - 'A' + 10); else bits[n] = (bits[n] << 4) | (s[i + 1] - '0'); ++n; } return n; } int main(void) { char s[] = "E4F1C3A81F"; char bits[10]; int i, n = Change(s, bits); printf("%s\n", s); for (i = 0; i < n; i++) printf("%X ", 0XFF & bits[i]); printf("\n"); return 0; }(2)、
int hexCharToInt(char c) { if (c >= '0' && c <= '9') return (c - '0'); if (c >= 'A' && c <= 'F') return (c - 'A' + 10); if (c >= 'a' && c <= 'f') return (c - 'a' + 10); return 0; } char* hexstringToBytes(string s) { int sz = s.length(); char *ret = new char[sz / 2]; for (int i = 0; i <sz; i += 2) { ret[i / 2] = (char)((hexCharToInt(s.at(i)) << 4) | hexCharToInt(s.at(i + 1))); } return ret; }