std::string EncodeUtf8(std::wstring in)
{
std::string s(in.length() * 3 + 1, ‘ ‘);
size_t len = ::WideCharToMultiByte(CP_UTF8, 0,
NULL, NULL);
}
// UTF8 转宽字符
std::wstring DecodeUtf8(string in)
{
wstring s(in.length(), (‘ ‘)); //必需得添加“tchar.h“头文件
size_t len = ::MultiByteToWideChar(CP_UTF8, 0,
return s;
}
std::string ws2s(const std::wstring& ws)
{
std::string curLocale = setlocale(LC_ALL, NULL); // curLocale = “C”;
const wchar_t* _Source = ws.c_str();
size_t _Dsize = 2 * ws.size() + 1;
char *_Dest = new char[_Dsize];
wcstombs(_Dest, _Source, _Dsize);
setlocale(LC_ALL, curLocale.c_str());
}
//string 转wstring
std::wstring s2ws(const std::string& s)
{
const char* _Source = s.c_str();
size_t _Dsize = s.size() + 1;
wchar_t *_Dest = new wchar_t[_Dsize];
mbstowcs(_Dest, _Source, _Dsize);
}
// 如:{0xC8, 0x32, 0x9B, 0xFD, 0x0E, 0x01} –> “C8329BFD0E01”
// nSrcLength – 源数据长度
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 = ”; // 返回目标字符串长度 return (nSrcLength * 2);
}
int gstringtobytes(std::string strsrc, std::vector& vec)
{
ASSERT(strsrc.empty() != false);
std::istringstream ism(strsrc);
istream_iterator it(ism), iend;
//transform(first,last,result,op);
transform(it, iend, back_inserter(vec), [](unsigned u) {
return (BYTE)u; });
}
// 如:”C8329BFD0E01″ –> {0xC8, 0x32, 0x9B, 0xFD, 0x0E, 0x01}
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’)
}
{
*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(“%sn”,intSprint); return 0;
}
原文地址:https://blog.csdn.net/sukeman/article/details/134631309
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_16839.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!