C++ String 字符串系列转换

// 宽字符转UTF8 ,处理字符乱码

std::string EncodeUtf8(std::wstring in)

{

std::string s(in.length() * 3 + 1, ' ');

size_t len = ::WideCharToMultiByte(CP_UTF8, 0,

in.c_str(), in.length(),

&s[0], s.length(),

NULL, NULL);

s.resize(len);

return s;

}

// UTF8 转宽字符

std::wstring DecodeUtf8(string in)

{

wstring s(in.length(), (' ')); //必需得添加"tchar.h"头文件

size_t len = ::MultiByteToWideChar(CP_UTF8, 0,

in.c_str(), in.length(),

&s[0], s.length());

s.resize(len);

return s;

}

//wstring转string

std::string ws2s(const std::wstring& ws)

{

std::string curLocale = setlocale(LC_ALL, NULL); // curLocale = "C";

setlocale(LC_ALL, "chs");

const wchar_t* _Source = ws.c_str();

size_t _Dsize = 2 * ws.size() + 1;

char *_Dest = new char[_Dsize];

memset(_Dest, 0, _Dsize);

wcstombs(_Dest, _Source, _Dsize);

std::string result = _Dest;

delete[]_Dest;

setlocale(LC_ALL, curLocale.c_str());

return result;

}

//string 转wstring

std::wstring s2ws(const std::string& s)

{

setlocale(LC_ALL, "chs");

const char* _Source = s.c_str();

size_t _Dsize = s.size() + 1;

wchar_t *_Dest = new wchar_t[_Dsize];

wmemset(_Dest, 0, _Dsize);

mbstowcs(_Dest, _Source, _Dsize);

std::wstring result = _Dest;

delete[]_Dest;

setlocale(LC_ALL, "C");

return result;

}

// 字节数据转换为可打印字符串

// 如:{0xC8, 0x32, 0x9B, 0xFD, 0x0E, 0x01} --> "C8329BFD0E01"

// 输入: pSrc - 源数据指针

// nSrcLength - 源数据长度

// 输出: pDst - 目标字符串指针

// 返回: 目标字符串长度

int gsmBytes2String(const unsigned char* pSrc, char* pDst, int nSrcLength)

{

const char tab[]="0123456789ABCDEF"; // 0x0-0xf的字符查找表

for (int i = 0; i < nSrcLength; i++) { *pDst++ = tab[*pSrc >> 4]; // 输出高4位 *pDst++ = tab[*pSrc & 0x0f]; // 输出低4位 pSrc++; } // 输出字符串加个结束符 *pDst = '\0'; // 返回目标字符串长度 return (nSrcLength * 2);

}

#include

#include

int gstringtobytes(std::string strsrc, std::vector& vec)

{

using namespace std;

ASSERT(strsrc.empty() != false);

std::istringstream ism(strsrc);

ism >> hex;

istream_iterator it(ism), iend;

std::vector vec;

//transform(first,last,result,op);

transform(it, iend, back_inserter(vec), [](unsigned u) {

return (BYTE)u; });

return vec.size();

}

// 可打印字符串转换为字节数据

// 如:"C8329BFD0E01" --> {0xC8, 0x32, 0x9B, 0xFD, 0x0E, 0x01}

// 输入: pSrc - 源字符串指针

// nSrcLength - 源字符串长度

// 输出: pDst - 目标数据指针

// 返回: 目标数据长度

int gsmString2Bytes(const char* pSrc, unsigned char* pDst, int nSrcLength)

{

for (int i = 0; i < nSrcLength; i += 2)

{

// 输出高4位

if ((*pSrc >= '0') && (*pSrc

{

*pDst = (*pSrc - '0')

}

else

{

*pDst = (*pSrc - 'A' + 10)

}

pSrc++; // 输出低4位 if ((*pSrc>='0') && (*pSrc<='9')) { *pDst |= *pSrc - '0'; } else { *pDst |= *pSrc - 'A' + 10; } pSrc++; pDst++; } // 返回目标数据长度 return (nSrcLength / 2);

}

int main(int argc, char* argv[])

{

char intSprint[255] = "C8329BFD0E0199999999";

unsigned char outBytes[255] = {0};

int szLength = 0;

int byLength = 0;

byLength = gsmString2Bytes(intSprint,outBytes,strlen(intSprint)); szLength = gsmBytes2String(outBytes,intSprint,strlen(intSprint)/2); printf("%s\n",intSprint); return 0;

}

你可能感兴趣的:(C++,c++)