string CStringHelper::Unicode2UTF8(const wstring& strUnicode)
{
int length = ::WideCharToMultiByte(CP_UTF8, 0, strUnicode.c_str(), -1, NULL, 0, NULL, NULL);
if (length <= 0) return "";
char* pText = new char[length + 1];
memset((void*)pText, 0, sizeof(char) * (length + 1));
::WideCharToMultiByte(CP_UTF8, 0, strUnicode.c_str(), -1, pText, length, NULL, NULL);
string strText(pText);
delete[] pText;
return strText;
}
wstring CStringHelper::UTF82Unicode(const string& strUTF8)
{
// multi char to wide char
int length = ::MultiByteToWideChar(CP_UTF8, 0, strUTF8.c_str(), -1, NULL, 0);
if (length <= 0) return L"";
wchar_t* pwText = new wchar_t[length + 1];
memset((void*)pwText, 0, sizeof(wchar_t) * (length + 1));
::MultiByteToWideChar(CP_UTF8, 0, strUTF8.c_str(), -1, pwText, length);
wstring strText(pwText);
delete[] pwText;
return strText;
}
string CStringHelper::GBKToUTF8(const string& strGBK)
{
int length = ::MultiByteToWideChar(CP_ACP, 0, strGBK.c_str(), -1, NULL, 0);
if (length <= 0) return "";
wchar_t* pText1 = new wchar_t[length];
::MultiByteToWideChar(CP_ACP, 0, strGBK.c_str(), -1, pText1, length);
length = WideCharToMultiByte(CP_UTF8, 0, pText1, -1, NULL, 0, NULL, NULL);
if (length <= 0)
{
delete [] pText1;
return "";
}
char* pText2 = new char[length];
::WideCharToMultiByte(CP_UTF8, 0, pText1, -1, pText2, length, NULL, NULL);
string strText(pText2);
delete[] pText1;
delete[] pText2;
return strText;
}
string CStringHelper::UTF8ToGBK(const string& strUTF8)
{
int length = MultiByteToWideChar(CP_UTF8, 0, strUTF8.c_str(), -1, NULL, 0);
if (length <= 0) return "";
unsigned short* pwText = new unsigned short[length + 1];
memset(pwText, 0, length * 2 + 2);
MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)strUTF8.c_str(), -1, (LPWSTR)pwText, length);
length = WideCharToMultiByte(CP_ACP, 0, (LPWSTR)pwText, -1, NULL, 0, NULL, NULL);
if (length <= 0)
{
delete [] pwText;
return "";
}
char* pText = new char[length + 1];
memset(pText, 0, length + 1);
WideCharToMultiByte(CP_ACP,0, (LPWSTR)pwText, -1, pText, length, NULL, NULL);
string strText(pText);
delete[]pText;
delete[]pwText;
return strText;
}
string CStringHelper::Unicode2ACSII(const wstring& strUnicode)
{
if (strUnicode.empty()) return "";
int length = ::WideCharToMultiByte(CP_ACP, 0, strUnicode.c_str(), -1, NULL, 0, NULL, NULL);
if (length <= 0) return "";
char* pText = new char[length + 1];
::WideCharToMultiByte(CP_ACP, 0, strUnicode.c_str(), -1, pText, length, NULL, NULL);
string strText(pText);
delete[] pText;
return strText;
}
wstring CStringHelper::ASCII2Unicode(const string& strASCII)
{
int length = MultiByteToWideChar(CP_ACP, 0, strASCII.c_str(), -1, NULL, 0);
if (length <= 0) return L"";
wchar_t* pText = new wchar_t[length + 1];
memset((void*)pText, 0, sizeof(wchar_t) * (length + 1));
::MultiByteToWideChar(CP_ACP, 0, strASCII.c_str(), -1, pText, length);
wstring strText(pText);
delete[] pText;
return strText;
}
int CStringHelper::SplitString(const string& strSrc, const string& strSplit, vector<string>& vecResult, bool bRemoveEmpty/* = false*/)
{
try
{
int pos = strSrc.find(strSplit, 0);
if (pos == -1)
{
// 如果strSrc没有分隔符,直接返回该字符串
if (strSrc.empty())
{
if (!bRemoveEmpty)
{
vecResult.push_back(strSrc);
}
}
else
{
vecResult.push_back(strSrc);
}
return vecResult.size();
}
int startPos = 0;
int splitN = pos;
string lineText;
while (pos > -1)
{
lineText = strSrc.substr(startPos, splitN);
startPos = pos + strSplit.length();
pos = strSrc.find(strSplit, startPos);
splitN = pos - startPos;
if (lineText.empty())
{
if (!bRemoveEmpty)
{
vecResult.push_back(lineText);
}
}
else
{
vecResult.push_back(lineText);
}
}
splitN = strSrc.length() - startPos;
if (splitN > 0) //添加分割线最后一个字符
{
lineText = strSrc.substr(startPos, splitN);
if (lineText.empty())
{
if (!bRemoveEmpty)
{
vecResult.push_back(lineText);
}
}
else
{
vecResult.push_back(lineText);
}
}
}
catch(exception e)
{
return 0;
}
return vecResult.size();
}
int CStringHelper::SplitString(const wstring& strSrc, const wstring& strSplit, vector<wstring>& vecResult, bool bRemoveEmpty/* = false*/)
{
try
{
int pos = strSrc.find(strSplit, 0);
if (pos == -1)
{
// 如果strSrc没有分隔符,直接返回该字符串
if (strSrc.empty())
{
if (!bRemoveEmpty)
{
vecResult.push_back(strSrc);
}
}
else
{
vecResult.push_back(strSrc);
}
return 0;
}
int startPos = 0;
int splitN = pos;
wstring lineText;
while (pos > -1)
{
lineText = strSrc.substr(startPos, splitN);
startPos = pos + strSplit.length();
pos = strSrc.find(strSplit, startPos);
splitN = pos - startPos;
Trim(lineText);
if (lineText.empty())
{
if (!bRemoveEmpty)
{
vecResult.push_back(lineText);
}
}
else
{
vecResult.push_back(lineText);
}
}
splitN = strSrc.length() - startPos;
if (splitN > 0) //添加分割线最后一个字符
{
lineText = strSrc.substr(startPos, splitN);
Trim(lineText);
if (lineText.empty())
{
if (!bRemoveEmpty)
{
vecResult.push_back(lineText);
}
}
else
{
vecResult.push_back(lineText);
}
}
}
catch(exception e)
{
return 0;
}
return vecResult.size();
}
int CStringHelper::sSplit(char* str, const char* delim, char* vec[], size_t vecLen, int dontSaveEmptyFields, int ignoreLeading)
{
unsigned char isDelim[256];
unsigned char charType;
int vecCount = 0;
if (!str)
{
return(-1);
}
/* Build is_delim array */
memset(isDelim, '\0', sizeof(isDelim));
if (!delim)
{
delim = " \t"; /* default field separators */
}
while (*delim)
{
isDelim[(unsigned)(unsigned char)*delim++] = 1; /* separator */
}
isDelim[(unsigned)(unsigned char)'\0'] = 2; /* terminator */
isDelim[(unsigned)(unsigned char)'\n'] = 2; /* terminator */
/* Parse string */
if (ignoreLeading)
{
/* skip leading separators */
while (isDelim[(unsigned)(unsigned char)*str] == 1)
{
str++;
}
}
/* first pointer is the beginning of string */
/* Check if we want to save this field */
if ((!dontSaveEmptyFields)
|| (isDelim[(unsigned)(unsigned char)*str] == 0))
{
/*
* We want empty fields, or the first character in this
* field is not a delimiter or the end of string.
* So save it.
*/
if (vecCount >= vecLen)
{
return(-1); /* overflow */
}
vec[vecCount++] = (char *) str;
}
while ((charType = isDelim[(unsigned)(unsigned char)*str]) != 2)
{
if (charType == 1)
{
/* the char is a separator */
/* null terminate the substring */
*str++ = '\0';
/* Check if we want to save this field */
if ((!dontSaveEmptyFields) || (isDelim[(unsigned)(unsigned char)*str] == 0))
{
/*
* We want empty fields, or the first character in this
* field is not a delimiter or the end of string.
* So save it.
*/
if (vecCount >= vecLen)
{
return(-1); /* overflow */
}
vec[vecCount++] = (char *) str;
}
}
else
{
str++;
}
}
*str = '\0'; /* null terminate the substring */
#ifdef SSPLIT_VERBOSE
{
int i;
printf("dump %d strings\n", vecCount);
for (i = 0; i < vecCount; i++)
{
printf("%d '%s'\n", i, vec[i]);
}
}
#endif /* def SSPLIT_VERBOSE */
return(vecCount);
}
int CStringHelper::sSplitOnce(const string &str, const string &delim, vector<string> &vecResult)
{
if (str.empty() || delim.empty())
{
return 0;
}
vecResult.clear();
if (str.size() && str.find(delim.c_str()))
{
string strFirst = str.substr(0, str.find_first_of(delim));
string strLast = str.substr(str.find_first_of(delim) + 1, str.length() - str.find_first_of(delim) - 1);
vecResult.push_back(strFirst);
vecResult.push_back(strLast);
}
else
{
vecResult.push_back(str);
}
return vecResult.size();
}
static unsigned int UTF8Decode(char* s, unsigned int* pi)
{
unsigned int c;
int i = *pi;
/* one digit utf-8 */
if ((s[i] & 128) == 0)
{
c = (unsigned int)s[i];
i += 1;
}
else if ((s[i] & 224) == 192)
{
/* 110xxxxx & 111xxxxx == 110xxxxx */
c = (((unsigned int)s[i] & 31) << 6) +
((unsigned int)s[i+1] & 63);
i += 2;
}
else if ((s[i] & 240)== 224 )
{
/* 1110xxxx & 1111xxxx == 1110xxxx */
c = (((unsigned int)s[i] & 15) << 12 ) +
(((unsigned int)s[i+1] & 63) << 6 ) +
((unsigned int)s[i+2] & 63);
i += 3;
}
else if ((s[i] & 248) == 240 )
{
/* 11110xxx & 11111xxx == 11110xxx */
c = (((unsigned int)s[i] & 7) << 18) +
(((unsigned int)s[i+1] & 63) << 12) +
(((unsigned int)s[i+2] & 63) << 6) +
((unsigned int)s[i+3] & 63);
i+= 4;
}
else if ((s[i] & 252) == 248 )
{
/* 111110xx & 111111xx == 111110xx */
c = (((unsigned int)s[i] & 3) << 24) +
(((unsigned int)s[i+1] & 63) << 18) +
(((unsigned int)s[i+2] & 63) << 12) +
(((unsigned int)s[i+3] & 63) << 6) +
((unsigned int)s[i+4] & 63);
i += 5;
}
else if ((s[i] & 254) == 252 )
{
/* 1111110x & 1111111x == 1111110x */
c = (((unsigned int)s[i] & 1) << 30) +
(((unsigned int)s[i+1] & 63) << 24) +
(((unsigned int)s[i+2] & 63) << 18) +
(((unsigned int)s[i+3] & 63) << 12) +
(((unsigned int)s[i+4] & 63) << 6) +
((unsigned int)s[i+5] & 63);
i += 6;
}
else
{
c = '?';
i++;
}
*pi = i;
return c;
}
/**
** URL加密原理:遍历URL字符串,对每个字符,按照以下规则进行加密。
** 1.如果当前字符是数字,不加密;
** 2.如果当前字符是空格,则用"+"代替;
** 3.如果是字符,按照下面的规则加密:将1个字符加密成3个字符。
** a.第1个字符为:"%",表示加密标志;
** b.第2个字符为一个16进制的字符:hex[ch / 16],取倍数;
** c.第3个字符为一个16进制的字符:hex[ch % 16],取余数;
**/
string CStringHelper::UrlEncode(const string& strSrc)
{
static char hex[] = "0123456789ABCDEF";
string dst;
const size_t srcLen = strSrc.size();
for (size_t i = 0; i < srcLen; i++)
{
unsigned char ch = strSrc[i];
if (isalnum(ch)) // Don't encode number or character.
{
dst += ch;
}
else
{
if (' ' == ch) // ' ' encode to '+'.
{
dst += '+';
}
else // Encode others from one character to three.
{
dst += '%'; // Set the encode flag.
dst += hex[ch / 16]; // Multiple - 倍数
dst += hex[ch % 16]; // Remainder - 余数
}
}
}
return dst;
}
/**
** URL解密原理:
** 1.如果当前字符为数字,不需要解密;
** 2.如果当前字符为"+",则用空格" "代替;
** 3.如果当前字符为"%",而且后两个字符均为16进制数字,则需要按照下面的规则解密:
** a.将后两个16进制字符转换成10进制数字(c1-48:字符转换成数字);
** b.这两个数字分别代表倍数和余数,然后转换为原来的数字:c1*16+c3;
**/
string CStringHelper::UrlDecode(const string& strSrc)
{
string strDst;
size_t srclen = strSrc.size();
for (size_t i = 0; i < srclen; i++)
{
if (strSrc[i] == '%') // Find the encode flag.
{
// isxdigit:是否为16进制数字
if(isxdigit(strSrc[i + 1]) && isxdigit(strSrc[i + 2])) // Hexadecimal number.
{
// 16进制转换为10进制
char c1 = strSrc[++i];
char c2 = strSrc[++i];
// 0 ANSI Code: 48;
// The counts from 58 to 65: 7;
// The counts from 65 to 97: 32;
c1 = c1 - 48 - ((c1 >= 'A') ? 7 : 0) - ((c1 >= 'a') ? 32 : 0);
c2 = c2 - 48 - ((c2 >= 'A') ? 7 : 0) - ((c2 >= 'a') ? 32 : 0);
strDst += (unsigned char)(c1 * 16 + c2); // Multiple * 16 + Remainder.
}
}
else
{
if (strSrc[i] == '+')
{
strDst += ' ';
}
else
{
strDst += strSrc[i];
}
}
}
string strDstUrl;
unsigned int len = strDst.size();
for (unsigned int pos = 0; pos < len;)
{
unsigned int nvalue = UTF8Decode((char*)strDst.c_str(), &pos);
strDstUrl += (unsigned char)nvalue;
}
return strDstUrl;
}
wstring CStringHelper::UrlDecodeEx(const string &strSrc)
{
string strDst;
wstring strDstUrl;
size_t srclen = strSrc.size();
for (size_t i = 0; i < srclen; i++)
{
if (strSrc[i] == '%')
{
if(isxdigit(strSrc[i + 1]) && isxdigit(strSrc[i + 2]))
{
char c1 = strSrc[++i];
char c2 = strSrc[++i];
c1 = c1 - 48 - ((c1 >= 'A') ? 7 : 0) - ((c1 >= 'a') ? 32 : 0);
c2 = c2 - 48 - ((c2 >= 'A') ? 7 : 0) - ((c2 >= 'a') ? 32 : 0);
strDst += (unsigned char)(c1 * 16 + c2);
}
}
else
{
if (strSrc[i] == '+')
{
strDst += ' ';
}
else
{
strDst += strSrc[i];
}
}
}
unsigned int len = strDst.size();
for(unsigned int pos = 0; pos < len;)
{
unsigned int nvalue = UTF8Decode((char *)strDst.c_str(), &pos);
strDstUrl += (unsigned wchar_t)nvalue;
}
return strDstUrl;
}
wstring CStringHelper::GetSubString(const wstring& strSrc, const wstring& strPattern)
{
wstring strSub;
try
{
if (strSrc.empty())
{
return L"";
}
const int pos = strSrc.find(strPattern);
if (wstring::npos == pos)
{
return strSrc;
}
strSub = strSrc.substr(0, pos);
}
catch(exception e)
{
return L"";
}
return strSub;
}
bool CStringHelper::LTrim(wstring& wstr,wchar_t sp)
{
if(!wstr.empty())
{
wchar_t* tmp_head = new wchar_t[wstr.length() + 1];
memcpy(tmp_head,wstr.c_str(),wstr.length() * sizeof(wchar_t));
tmp_head[wstr.length()] = '\0';
wchar_t *p, *q, *r;
p = tmp_head + wstr.length();
/*
* find end of leading whitespace
*/
q = r = tmp_head;
while (*q && (*q == sp))
{
q++;
}
/*
* if there was any, move the rest forwards
*/
if (q != tmp_head)
{
while (q <= p )
{
*r++ = *q++;
}
}
wstr.clear();
wstr = tmp_head;
delete tmp_head;
}
return true;
}
bool CStringHelper::RTrim(wstring& wstr,wchar_t sp)
{
if(!wstr.empty())
{
wchar_t* tmp_head = new wchar_t[wstr.length() + 1];
memcpy(tmp_head,wstr.c_str(),wstr.length() * sizeof(wchar_t));
tmp_head[wstr.length()] = '\0';
/*
* strip trailing whitespace
*/
wchar_t* p = tmp_head + wstr.length();
while (p > tmp_head && *(p-1) == sp)
{
p--;
}
*p = '\0';
wstr.clear();
wstr = tmp_head;
delete tmp_head;
}
return true;
}
bool CStringHelper::Trim(wstring& wstr,wchar_t sp)
{
if(!wstr.empty())
{
wchar_t* tmp_head = new wchar_t[wstr.length() + 1];
memcpy(tmp_head,wstr.c_str(),wstr.length() * sizeof(wchar_t));
tmp_head[wstr.length()] = '\0';
wchar_t *p, *q, *r;
/*
* strip trailing whitespace
*/
p = tmp_head + wstr.length();
while (p > tmp_head && *(p-1) == sp)
{
p--;
}
*p = '\0';
/*
* find end of leading whitespace
*/
q = r = tmp_head;
while (*q && (*q == sp))
{
q++;
}
/*
* if there was any, move the rest forwards
*/
if (q != tmp_head)
{
while (q <= p)
{
*r++ = *q++;
}
}
wstr.clear();
wstr = tmp_head;
delete tmp_head;
}
return true;
}
#define CONVERT_BUF_SIZE 20
string CStringHelper::Int2Str(int val)
{
char szStr[CONVERT_BUF_SIZE] = {0};
_itoa(val, szStr, 10);
return string(szStr);
}
wstring CStringHelper::Int2WStr(int val)
{
return ASCII2Unicode(Int2Str(val));
}
string CStringHelper::Long2Str(long val)
{
char szStr[CONVERT_BUF_SIZE] = {0};
_ltoa(val, szStr, 10);
return string(szStr);
}
wstring CStringHelper::Long2WStr(long val)
{
return ASCII2Unicode(Long2Str(val));
}
void CStringHelper::Replace(wstring &strInput, const wstring &strSrc, const wstring &strDst)
{
if (strInput.empty())
{
return;
}
wstring::size_type pos = 0;
wstring::size_type srclen = strSrc.size();
wstring::size_type dstlen = strDst.size();
while((pos = strInput.find(strSrc, pos)) != wstring::npos)
{
strInput.replace(pos, srclen, strDst);
pos += dstlen;
}
}
bool CStringHelper::BeginWith(wstring strSrc, const wstring str)
{
if (strSrc.empty())
{
return false;
}
wstring::size_type pos = wstring::npos;
if((pos = strSrc.find(str)) == 0)
{
return true;
}
return false;
}
bool CStringHelper::EndWith(wstring strSrc, const wstring str)
{
if (strSrc.empty())
{
return false;
}
wstring::size_type pos = strSrc.rfind(str);
if( pos == (strSrc.length() - str.length()))
{
return true;
}
return false;
}
wstring CStringHelper::StringCreateEscapes(const wstring& strSrc)
{
wstring result;
wstring::const_iterator b = strSrc.begin();
wstring::const_iterator e = strSrc.end();
while (b != e)
{
if (*b == 0x20 || *b == 0x21 || (*b >= 0x23 && *b <= 0x2E) ||
(*b >= 0x30 && *b <= 0x5B) || (*b >= 0x5D && *b <= 0xFF))
result += *b;
else if (*b == L'\b') result += L'\\', result += L'b';
else if (*b == L'\f') result += L'\\', result += L'f';
else if (*b == L'\n') result += L'\\', result += L'n';
else if (*b == L'\r') result += L'\\', result += L'r';
else if (*b == L'/') result += L'\\', result += L'/';
else if (*b == L'"') result += L'\\', result += L'"';
else if (*b == L'\\') result += L'\\', result += L'\\';
else
{
const wchar_t *hexdigits = L"0123456789ABCDEF";
typedef make_unsigned<wchar_t>::type UCh;
unsigned long u = (std::min)(static_cast<unsigned long>(
static_cast<UCh>(*b)),
0xFFFFul);
int d1 = u / 4096; u -= d1 * 4096;
int d2 = u / 256; u -= d2 * 256;
int d3 = u / 16; u -= d3 * 16;
int d4 = u;
result += L'\\'; result += L'u';
result += hexdigits[d1]; result += hexdigits[d2];
result += hexdigits[d3]; result += hexdigits[d4];
}
++b;
}
return result;
}
string CStringHelper::StringCreateEscapes(const string& strSrc)
{
string result;
string::const_iterator b = strSrc.begin();
string::const_iterator e = strSrc.end();
while (b != e)
{
if (*b == 0x20 || *b == 0x21 || (*b >= 0x23 && *b <= 0x2E) ||
(*b >= 0x30 && *b <= 0x5B) || (*b >= 0x5D && *b <= 0xFF))
result += *b;
else if (*b == '\b') result += '\\', result += 'b';
else if (*b == '\f') result += '\\', result += 'f';
else if (*b == '\n') result += '\\', result += 'n';
else if (*b == '\r') result += '\\', result += 'r';
else if (*b == '/') result += '\\', result += '/';
else if (*b == '"') result += '\\', result += '"';
else if (*b == '\\') result += '\\', result += '\\';
else
{
const char *hexdigits = "0123456789ABCDEF";
typedef make_unsigned<char>::type UCh;
unsigned long u = (std::min)(static_cast<unsigned long>(
static_cast<UCh>(*b)),
0xFFFFul);
int d1 = u / 4096; u -= d1 * 4096;
int d2 = u / 256; u -= d2 * 256;
int d3 = u / 16; u -= d3 * 16;
int d4 = u;
result += '\\'; result += 'u';
result += hexdigits[d1]; result += hexdigits[d2];
result += hexdigits[d3]; result += hexdigits[d4];
}
++b;
}
return result;
}
wstring CStringHelper::Lower2Capital(const wstring& lower)
{
return _wcsupr(const_cast<wchar_t*>(lower.c_str()));
}
wstring CStringHelper::Capital2Lower(const wstring& capital)
{
return _wcslwr(const_cast<wchar_t*>(capital.c_str()));
}
wstring CStringHelper::GenerateRandStr(const int len/* = 32*/)
{
wstring strRet(L"", len);
static bool bTmp = true;
if (bTmp)
{
// 种子只需设置一次
srand(GetTickCount());
bTmp = false;
}
for (int i = 0; i < len; i++)
{
strRet.at(i) = rand() % 26 + 'a';
}
return strRet;
}
// 生成随机字符串的源数据
wchar_t srcStrArr[] = L"0123456789abcdefghijklmnopqrstuvwxyz";
wstring CStringHelper::GenerateRandStrEx(const int len/* = 32*/)
{
wstring strRet(L"", len);
static bool bTmp = true;
if (bTmp)
{
// 种子只需设置一次
srand(GetTickCount());
bTmp = false;
}
for (int i = 0; i < len; i++)
{
strRet.at(i) = srcStrArr[rand() % 36]; // 36 = 10 + 26
}
return strRet;
}
string CStringHelper::DecryptString(const string& strSrc, const string& strKey /* = L"" */)
{
string strS = strSrc;
string strK = strKey;
string strValue = CRyptLib::Decode(strS, strK);
return strValue;
}
string CStringHelper::EncryptString(const string& strSrc, const string& strKey /* = L"" */)
{
string strS = strSrc;
string strK = strKey;
string strValue = CRyptLib::Encode(strS, strK);
return strValue;
}
wstring CStringHelper::DecryptString(const wstring& strSrc, const wstring& strKey /* = L"" */)
{
string strS = Unicode2ACSII(strSrc);
string strK = Unicode2ACSII(strKey);
string strValue = CRyptLib::Decode(strS, strK);
wstring strResult = ASCII2Unicode(strValue);
return strResult;
}
wstring CStringHelper::EncryptString(const wstring& strSrc, const wstring& strKey /* = L"" */)
{
string strS = Unicode2ACSII(strSrc);
string strK = Unicode2ACSII(strKey);
string strValue = CRyptLib::Encode(strS, strK);
wstring strResult = ASCII2Unicode(strValue);
return strResult;
}
一些编码格式的字符串转换
最新推荐文章于 2022-05-19 13:14:05 发布