1. 程式人生 > >C/C++字串總結

C/C++字串總結

一、概念

ANSI:單位元組字元,佔1個位元組,最多能表示256個符號。
DBCS(Double-Byte Character Set):每個字元佔1到2個位元組。
Unicode:每個字元佔2個位元組。
UTF-8:將一些字元編碼為1個位元組,一些字元編碼為2個位元組,一些字元編碼為3個位元組,一些字元編碼為4個位元組,1個位元組–美國字元,2個位元組–歐洲和中東地區,3個位元組–東亞地區,4個位元組–代理對(Surrogate Pair)。
UTF-32:將每個字元都編碼為4個位元組,不是一種高效的編碼格式,將字串儲存到檔案或者傳到網路時,很少用這個格式,這種編碼格式一般在應用程式內部使用。

C語言用char來表示一個8位ANSI字元,用wchar_t表示一個16位的Unicode字元。
strlen返回一個ANSI字串的長度,wcslen返回一個Unicode字串的長度。

char表示一個ANSI字元。
wchar_t表示一個Unicode字元。
std::string表示一個ANSI字串。
std::wstring表示一個Unicode字串。

“winnt.h”
typedef char CHAR;

typedef CHAR *PCHAR, *LPCH, *PCH;
typedef __nullterminated CHAR *NPSTR, *LPSTR, *PSTR;
typedef __nullnullterminated CHAR *PZZSTR;
typedef __possibly_notnullterminated CHAR *PNZCH;

typedef CONST CHAR *LPCCH, *PCCH;
typedef __nullterminated CONST CHAR *LPCSTR, *PCSTR;
typedef __nullnullterminated CONST CHAR *PCZZSTR;
typedef __possibly_notnullterminated CONST CHAR *PCNZCH;

typedef __nullterminated PSTR *PZPSTR;
typedef __nullterminated CONST PSTR *PCZPSTR;

typedef __nullterminated PCSTR *PZPCSTR;
注:__nullterminated表示一個以’\0’結尾的字串,__nullnullterminated表示一個以兩個’\0’結尾的字串,__possibly_notnullterminated表示一個不以’\0’結尾的字串。

typedef WCHAR *PWCHAR, *LPWCH, *PWCH;
typedef CONST WCHAR *LPCWCH, *PCWCH;

typedef WCHAR TCHAR, *PTCHAR;
typedef WCHAR TBYTE, *PTBYTE;

typedef __nullterminated WCHAR *NWPSTR, *LPWSTR, *PWSTR;
typedef __nullterminated PWSTR *PZPWSTR;
typedef __nullterminated CONST PWSTR *PCZPWSTR;
typedef __nullterminated WCHAR UNALIGNED *LPUWSTR, *PUWSTR;
typedef __nullterminated CONST WCHAR *LPCWSTR, *PCWSTR;
typedef __nullterminated PCWSTR *PZPCWSTR;
typedef __nullterminated CONST WCHAR UNALIGNED *LPCUWSTR, *PCUWSTR;

typedef __nullnullterminated WCHAR *PZZWSTR;
typedef __nullnullterminated CONST WCHAR *PCZZWSTR;
typedef __nullnullterminated WCHAR UNALIGNED *PUZZWSTR;
typedef __nullnullterminated CONST WCHAR UNALIGNED *PCUZZWSTR;

typedef __possibly_notnullterminated WCHAR *PNZWCH;
typedef __possibly_notnullterminated CONST WCHAR *PCNZWCH;
typedef __possibly_notnullterminated WCHAR UNALIGNED *PUNZWCH;
typedef __possibly_notnullterminated CONST WCHAR UNALIGNED *PCUNZWCH;

typedef LPWCH LPTCH, PTCH;
typedef LPCWCH LPCTCH, PCTCH;
typedef LPWSTR PTSTR, LPTSTR;
typedef LPCWSTR PCTSTR, LPCTSTR;
typedef LPUWSTR PUTSTR, LPUTSTR;
typedef LPCUWSTR PCUTSTR, LPCUTSTR;
typedef LPWSTR LP;
typedef PZZWSTR PZZTSTR;
typedef PCZZWSTR PCZZTSTR;
typedef PUZZWSTR PUZZTSTR;
typedef PCUZZWSTR PCUZZTSTR;
typedef PNZWCH PNZTCH;
typedef PCNZWCH PCNZTCH;
typedef PUNZWCH PUNZTCH;
typedef PCUNZWCH PCUNZTCH;

根據型別定義,直接給出常見的字串型別所對應的具體型別

LPSTR – char*

LPWSTR – wchar_t*

LPTSTR – char* 或 wchar_t*

LPCSTR – const char*

LPCWSTR – const wchar_t*

LPCTSTR – const wchar_t* 或 const char*

typedef unsigned char byte;
typedef unsigned char BYTE;
typedef BYTE near *PBYTE;
typedef BYTE far *LPBYTE;
注:BYTE和byte就代表一個位元組。

COM中的字串
BSTR是COM/OLE中定義的標準字串資料型別。
對於C++,定義在”wtypes.h”中。
typedef wchar_t WCHAR;
typedef WCHAR OLECHAR;
typedef OLECHAR __RPC_FAR *BSTR;
BSTR是一個有長度字首和NULL結束符的OLECHAR陣列。
BSTR的前4個位元組是一個表示字串長度的字首,長度域的值不包含結束符。
由於是Unicode串,所以字串是位元組數的一半。
BSTR的前四個位元組表示長度,而OLECHAR陣列的前四個位元組表示前兩個字元,因此,BSTR和OLECHAR的轉換是一個問題。
COM提供了兩個BSTR分配用的API:SysAllocString()和SysReallocString(),函式返回的指標指向BSTR的第一個字元,而不是BSTR在記憶體的第一個位元組。

_bstr_t是一個封裝並管理BSTR的類,通過這個類,可以方便的把C++型別轉換成COM中的變數。
_bstr_t有點類似於只能指標,使用了引用計數。

CComBSTR提供了對BSTR的封裝。

二、型別轉換[根據目錄通過索引查詢]

1.std::string與std::wstring相互轉換

2.char*與wchar_t*相互轉換

3.char*與BSTR相互轉換

4.char*與_bstr_t相互轉換

5.char*與CComBSTR相互轉換

6.BSTR與_bstr_t相互轉換

7.BSTR與CComBSTR相互轉換

8._bstr_t與CComBSTR相互轉換

9.char*與std::string相互轉換

10.wchar_t*與std::wstring相互轉換

1

std::wstring StdstringToStdwstring(const std::string& szStr)
{
    // Get the length of buffer we'll need for the string
    const int nStdwstringLengthBytes = ::MultiByteToWideChar(CP_UTF8, 0, szStr.c_str(), -1, NULL,
        /*give us the length of buffer we'll need for the encoded string*/0);
    if (nStdwstringLengthBytes <= 0 )
    { // Record Error
        return NULL;
    }


    // And now actually perform the conversion...
    wchar_t* wszStr = new wchar_t[nStdwstringLengthBytes];
    const int nStdstringLengthBytes = ::MultiByteToWideChar(
        CP_UTF8, 0, szStr.c_str(), -1, wszStr, nStdwstringLengthBytes);


    if (nStdstringLengthBytes != nStdwstringLengthBytes)
    { // Record Error
        return NULL;
    }


    return std::wstring(wszStr);
}


std::string StdwstringToStdstring(const std::wstring& wszStr)
{
    // Get the length of buffer we'll need for the string
    const int nStdstringLengthBytes = ::WideCharToMultiByte(CP_UTF8, 0, wszStr.c_str(), -1, NULL,
        /*give us the length of buffer we'll need for the encoded string*/0, NULL, NULL);
    if (nStdstringLengthBytes <= 0)
    { // Record Error
        return NULL;
    }


    // And now actually perform the conversion...
    char* szStr = new char[nStdstringLengthBytes];
    const int nStdwstringLengthBytes = ::WideCharToMultiByte(
        CP_UTF8, 0, wszStr.c_str(), -1, szStr, nStdstringLengthBytes, NULL, NULL);
    if (nStdwstringLengthBytes != nStdstringLengthBytes)
    { // Record Error
        return NULL;
    }


    return std::string(szStr);
}

2

wchar_t* PccharToPwcchart(const char* pszStr)
{
    size_t nLen = strlen(pszStr) + 1;
    size_t nConverted = 0;


    wchar_t* pwszStr = (wchar_t*)malloc(nLen * sizeof(wchar_t));
    mbstowcs_s(&nConverted, pwszStr, nLen, pszStr, _TRUNCATE);


    return pwszStr;
}


char* PwcchartToPcchar(const wchar_t* pwszStr)
{
    size_t nLen = wcslen(pwszStr) + 1;
    size_t nConverted = 0;


    char* pszStr = (char*)malloc(nLen * sizeof(char));
    wcstombs_s(&nConverted, pszStr, nLen, pwszStr, _TRUNCATE);


    return pszStr;
}

3

BSTR PccharToBSTR(const char* pszStr)
{
    if (NULL == pszStr)
    { // Record Error
        return NULL;
    }


    BSTR bstr = _bstr_t(pszStr);


    return bstr;
}


char* BSTRToPcchar(const BSTR bstr)
{
    char* pszStr = _bstr_t(bstr);


    return pszStr;
}

4

_bstr_t PccharTobstrt(const char* pszStr)
{
    if (NULL == pszStr)
    { // Record Error
        return "";
    }


    _bstr_t bstrt(pszStr);


    return bstrt;
}


char* bstrtToPcchar(const _bstr_t bstrt)
{
    return bstrt;
}

5

CComBSTR PccharToCComBSTR(const char* pszStr)
{
    if (NULL == pszStr)
    { // Record Error
        return NULL;
    }


    CComBSTR str(pszStr);


    return str;
}


char* CComBSTRToPcchar(const CComBSTR str)
{
    return const_cast<char*>(static_cast<const char*>(str));
}

6

_bstr_t BSTRTobstrt(const BSTR bstr)
{
    _bstr_t bstrt(bstr);


    return bstrt;
}


BSTR bstrtToBSTR(const _bstr_t bstrt)
{
    return static_cast<BSTR>(bstrt);
}

7

CComBSTR BSTRToCComBSTR(const BSTR bstr)
{
    CComBSTR str(bstr);


    return str;
}


BSTR CComBSTRToBSTR(const CComBSTR str)
{
    return static_cast<BSTR>(str);
}

8

CComBSTR bstrtToCComBSTR(const _bstr_t bstr)
{
    CComBSTR str(static_cast<BSTR>(bstr));


    return str;
}


_bstr_t CComBSTRTobstrt(const CComBSTR str)
{
    _bstr_t bstrt(static_cast<BSTR>(bstr));


    return bstrt;
}

9

std::string PccharToStdstring(const char* pszStr)
{
    return pszStr;
}


char* StdstringToPcchar(const std::string szStr)
{
    return const_cast<char*>(szStr.c_str());
}

10

std::wstring PwccharToStdwstring(const wchar_t* pwszStr)
{
    return pwszStr;
}


wchar_t* StdwstringToPwcchar(const std::wstring wszStr)
{
    return const_cast<wchar_t*>(wszStr.c_str());
}