// 宽字符转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 &amp; 0x0f]; // 输出低4位 pSrc++; } // 输出字符串加个结束符 *pDst = ”; // 返回目标字符长度 return (nSrcLength * 2);

}

#include

#include

int gstringtobytes(std::string strsrc, std::vector&amp; 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’) &amp;&amp; (*pSrc

{

*pDst = (*pSrc – ‘0’)

}

else

{

*pDst = (*pSrc – ‘A’ + 10)

}

pSrc++; // 输出低4位 if ((*pSrc>=’0′) &amp;&amp; (*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(“%sn”,intSprint); return 0;

}

原文地址:https://blog.csdn.net/sukeman/article/details/134631309

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_16839.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注