C++ 字符串转换

来源:互联网 发布:淘宝上如何搜索咸鱼 编辑:程序博客网 时间:2024/06/14 01:04

一:UtilString.h

#pragma once

#include <string>

#include <WTypes.h>

namespace UtilString{
    std::wstring      ConvertMultiByteToWideChar(const std::string& strSrc);
    std::wstring      ConvertMultiByteToWideCharUTF8(const std::string& strSrc);

    std::string       ConvertWideCharToMultiByte(const std::wstring& wstrSrc);
    std::string       ConvertWideCharToMultiByteUTF8(const std::wstring& wstrSrc);

    void              WideToMultiByte(std::wstring &strSrc, char* strDes);

    bool              FindStringInString(const std::wstring& strSrc0, const std::wstring& strToFind0);
    bool              CheckStringIsNumber(const std::wstring& wstr, int& number);

    std::string       StringMakeLower(const std::string& strSrc);
    std::string       StringMakeUpper(const std::string& strSrc);
    std::wstring      StringMakeUpper(const std::wstring& strSrc);
    std::wstring      StringMakeLower(const std::wstring& strSrc);

    std::wstring      NumberToWstring(int number);

    UINT              ConvertStringToNumber(const std::wstring& wstr);
    UINT              ConvertHexStringToNumber(const std::wstring& wstrOld);
    int               GetNumberFromString(const std::wstring& str);

    void              SplitStringByChar(CString allStr, TCHAR x, std::vector<std::wstring> &strArray);
    void              SplitString(const std::string& source, const char* sep, std::vector<int>& resultlist);
    void              SplitString(const std::string& source, const char* sep, std::vector<double>& resultlist);

}


二:UtilString.cpp

#include "stdafx.h"

#include "UtilString.h"

#include <tchar.h>
#include <windows.h>

using namespace std;

//////////////////////////////////////////////////////////////////////////
namespace UtilString{

    //must free LPWSTR after call this func
    std::wstring ConvertMultiByteToWideChar(const std::string& strSrc)                    //string to wstring
    {
        const char* pStr = strSrc.c_str();
        //size_t nLenOld = strSrc.length();
        const int nLen = (int)strSrc.length();

        wstring retStr;
        TCHAR* pwstr = NULL;
        int len=0;

        len=MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED,pStr,nLen+1,pwstr,0);
        if(len==0)
            return retStr;

        pwstr=(LPWSTR)malloc(len*sizeof(WCHAR));
        len=MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED,pStr,nLen+1,pwstr,len);
        if(len==0)
        {
            free(pwstr);
            return retStr;
        }
        retStr = pwstr;
        free(pwstr);

        return retStr;
    }

    std::string ConvertWideCharToMultiByte(const std::wstring& wstrSrc)         //wstring  to  string                            
    {
        const TCHAR* pWStr = wstrSrc.c_str();
        const int nLen = (int)wstrSrc.length();
        string ret;
        LPSTR pStr=0;
        int len=WideCharToMultiByte(CP_ACP,0,pWStr,nLen+1,0,0,0,0);
        if(!len)
            return ret;        

        pStr=(LPSTR)malloc(len);

        len=WideCharToMultiByte(CP_ACP,0,pWStr,nLen+1,pStr,len,0,0);
        if(!len)
        {
            free(pStr);
            return ret;        
        }
        ret = pStr;
        free(pStr);

        return ret;
    }

    wstring ConvertMultiByteToWideCharUTF8(const std::string& strSrc)
    {
        const char* pStr = strSrc.c_str();
        const int nLen = (int)strSrc.length();
        LPWSTR pwstr=NULL;
        wstring wstrRet;
        int len=0;

        len=MultiByteToWideChar(CP_UTF8,0,pStr,nLen+1,pwstr,0);
        if(len==0)
            return 0;

        pwstr=(LPWSTR)malloc(len*sizeof(WCHAR));
        len=MultiByteToWideChar(CP_UTF8,0,pStr,nLen+1,pwstr,len);
        if(len==0)
        {
            free(pwstr);
            return wstrRet;
        }
        pwstr[len-1] = 0;//Added by Kevin, 2008/08/19, in order to set pwstr to null terminated string
        wstrRet = pwstr;

        return wstrRet;
    }

    //must free LPWSTR after call this func
    string ConvertWideCharToMultiByteUTF8(const std::wstring& wstrSrc)
    {
        const TCHAR* pWStr = wstrSrc.c_str();
        const int nLen = (int)wstrSrc.length();
        LPSTR pStr=0;
        string strRet;
        int len=0;

        //CP_ACP
        len=WideCharToMultiByte(CP_UTF8,0,pWStr,nLen+1,0,0,0,0);
        if(!len) //????
            return 0;        

        pStr=(LPSTR)malloc(len);

        //CP_ACP
        len=WideCharToMultiByte(CP_UTF8,0,pWStr,nLen+1,pStr,len,0,0);
        if(!len)
        {
            free(pStr);
            return strRet;        
        }
        strRet = pStr;
        return strRet;
    }

    //  the strDes must be the char array
    void WideToMultiByte(std::wstring &strSrc, char* strDes)
    {
        int i =  WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)strSrc.c_str(), -1, NULL, 0, NULL, NULL);
        WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)strSrc.c_str(), -1, strDes, i, NULL, NULL);
    }

    //make lower
    string  StringMakeLower(const string& strSrc)
    {        
        //_strlwr
        char* vecChar = new char[strSrc.length() +1];
        strcpy(vecChar, strSrc.c_str() ); //strSrc.length(),
        string strRet = _strlwr( vecChar);
        SAFE_DELETE_ARRAY(vecChar);
        return strRet;
    }

    wstring  StringMakeLower(const wstring& strSrc)
    {        
        TCHAR* vecChar = new TCHAR[strSrc.length() +1];
        _tcscpy(vecChar, strSrc.c_str() ); //strSrc.length(),
        wstring strRet = _wcslwr( vecChar);
        SAFE_DELETE_ARRAY(vecChar);
        return strRet;
    }

    //convert to low string. then search str in str
    bool FindStringInString(const wstring& strSrc0, const wstring& strToFind0)
    {
        bool ifFind = false;
        //CString strSrc1 = strSrc0.c_str();
        //strSrc1.MakeLower();
        //CString strToFind1 = strToFind0.c_str();
        //strToFind1.MakeLower();

        wstring strSrc1 = StringMakeLower(strSrc0);
        wstring strToFind1 = StringMakeLower(strToFind0);

        int findPos = (int)strSrc1.find(strToFind1);
        if(findPos >=0 && findPos < (int)strSrc1.length() )
            ifFind = true;
        return ifFind;
    }

    //str to d
    UINT ConvertStringToNumber(const wstring& wstr)
    {
        if(_T(" ") == wstr){
            return 0;
        }
        const string str0= ConvertWideCharToMultiByte(wstr );  
        int num = atoi( str0.c_str() );
        return num;
    }

    //sscanf("a0", "%02X", &num1);
    //get number from string like "0x84acef"
    UINT ConvertHexStringToNumber(const std::wstring& wstrOld)
    {
        UINT num = 0;
        //vector<TCHAR> tcharArray;
        const wstring strHex = _T("0123456789abcdefxxxxx");

        const wstring wstrLow = StringMakeLower(wstrOld);

        const UINT strLength = (UINT) wstrLow.length();
        int pos = -1;
        TCHAR ch;
        for(UINT idx =0; idx < strLength; idx++){
            ch = wstrLow.at(idx);
            pos = (int)strHex.find(ch);
            if(pos< 0
                || pos >= (int)strHex.length() ){
                    //ASSERT(false);
                    continue;
            }
            else if(pos >15){ }
            else{
                num = num*16 + pos;
            }
        }
        return num;
    }

    //字符串中有一段连着的数字
    int GetNumberFromString(const wstring& str)
    {
        UINT numberRet = -1;
        //CString strTemp;
        const UINT strLen = (UINT)str.length();
        int posStart = -1;
        int posEnd = -1;
        for(UINT idx= 0; idx< strLen; idx++){
            if(str[idx] >= _T('0') && str[idx] <= _T('9') ){
                if(posStart < 0){ //1st char
                    posStart = idx;               
                }
                else{
                }
                posEnd = idx;
            }
            else if(posStart >= 0){
                //end char
                break;
            }
            else{}
        }

        if(posStart < 0){
            //find no number
            return numberRet;
        }

        //ASSERT(posEnd - posStart + 1 >= 0 );
        if(posEnd - posStart + 1 < 0) return 0;
        wstring wstrSub = str.substr(posStart, posEnd - posStart + 1);
        string strSub = UtilString::ConvertWideCharToMultiByte(wstrSub);
        numberRet = atoi(strSub.c_str() );

        return numberRet;
    }

    std::wstring        NumberToWstring(int number)
    {
        //CString cstr;
        //cstr.Format(_T("%d"), number);
        char charvec[10];
        _itoa(number, charvec ,10);
        const string str = charvec;
        const wstring wstr = ConvertMultiByteToWideChar( str);
        return wstr;
    }

    //判断是否一个string全部是数字, 如有, 转换
    bool    CheckStringIsNumber(const std::wstring& wstrOld, int& number)
    {

        number = 0;

        const wstring strNumber = _T("0123456789-.");
        const wstring wstrLow = StringMakeLower(wstrOld);
        const UINT strLength = (UINT) wstrLow.length();

        int pos = -1;
        TCHAR ch;
        bool ifNumber =true;

        for(UINT idx =0; idx < strLength; idx++){
            ch = wstrLow.at(idx);
            pos = (int)strNumber.find(ch);
            if(pos< 0
                || pos >= (int)strNumber.length() ){
                    //ASSERT(false);
                    ifNumber =false;
                    break;
            }
        }
        if(ifNumber){
            number = ConvertStringToNumber(wstrOld);
        }

        return ifNumber;
    }
    //////////////////////////////////////////////////////////////////////////
    void SplitStringByChar(CString allStr, TCHAR x, std::vector<std::wstring> &strArray)
    {//split allStr to strArray
        // "3/4/5" "/" ->(3,4,5) array
        strArray.clear();
        if(allStr=="") return;
        int aa=allStr.Find(x);
        if(aa==-1)
        {
            strArray.push_back((LPCTSTR)allStr);//add Element//////
            return;
        }
        strArray.push_back((LPCTSTR)allStr.Left(aa));//add Element//////
        int bb=allStr.Find(x, aa+1);
        if(bb==-1)
        {
            strArray.push_back((LPCTSTR)allStr.Mid(aa+1));//add Element//////
            return;
        }
        while(bb!=-1)
        {
            strArray.push_back((LPCTSTR)allStr.Mid(aa+1,bb-aa-1));//add Element//////
            aa=bb;
            bb=allStr.Find(x,aa+1);//add Element//////
        }
        strArray.push_back((LPCTSTR)allStr.Mid(aa+1));//add Element//////    
    }

    void SplitString(const std::string& strsource, const char* sep, vector<int>& resultlist)
    {
        if( !sep)
            return;

        //assert(source != NULL);
        char* strcopy = _strdup(strsource.c_str() );
        //assert(strcopy != NULL);

        char* token = NULL;
        char* nexttoken = NULL;
        token = strtok_s(strcopy, sep, &nexttoken);

        int temp = -1;
        while(token != NULL)
        {
            temp = atoi(token);
            resultlist.push_back(temp);
            token = strtok_s(NULL, sep, &nexttoken);
        }

        free(strcopy);
    }

    void SplitString(const std::string& source, const char* sep, vector<double>& resultlist)
    {
        if( !sep)
            return;

        //assert(source != NULL);
        char* strcopy = _strdup(source.c_str());
        //assert(strcopy != NULL);

        char* token = NULL;
        char* nexttoken = NULL;
        token = strtok_s(strcopy, sep, &nexttoken);

        double temp = 0.0;
        while(token != NULL)
        {
            temp = atof(token);
            resultlist.push_back(temp);
            token = strtok_s(NULL, sep, &nexttoken);
        }

        free(strcopy);
    }
    //////////////////////////////////////////////////////////////////////////

}; //namespace

原创粉丝点击