UTF 8與多位元組字串轉化

2021-10-09 12:37:35 字數 3212 閱讀 2126

/ utf-8轉本地編碼以及本地編碼轉utf-8 ///

// 寬字元字串轉多位元組字串

inline std::string _w2a_

(const

wchar_t

* pwsztext)

int isizeinbytes =

widechartomultibyte

(cp_acp,

0, pwsztext,-1

,null,0

,null

,null);

char

* pmultibyte =

new(std::nothrow)

char

[isizeinbytes];if

(pmultibyte ==

null

)memset

(pmultibyte,

0, isizeinbytes)

;widechartomultibyte

(cp_acp,

0, pwsztext,-1

, pmultibyte, isizeinbytes,

null

,null);

std::string strresult = std::

string

(pmultibyte)

;delete

pmultibyte;

pmultibyte =

null

;return strresult;

}// utf-8字串轉寬字元字串

inline std::wstring _u82w_

(const

char

* psztext)

int isizeinchars =

multibytetowidechar

(cp_utf8,

0, psztext,-1

,null,0

);wchar_t

* pwidechar =

new(std::nothrow)

wchar_t

[isizeinchars];if

(pwidechar ==

null

)wmemset

(pwidechar,

0, isizeinchars)

;multibytetowidechar

(cp_utf8,

0, psztext,-1

, pwidechar, isizeinchars)

; std::wstring strresult = std::

wstring

(pwidechar)

;delete

pwidechar;

pwidechar =

null

;return strresult;

}// utf-8字串轉多位元組字串

inline std::string _u82a_

(const

char

* psztext)

// 多位元組字串轉寬字元字串

inline std::wstring _a2w_

(const

char

* psztext)

int isizeinchars =

multibytetowidechar

(cp_acp,

0, psztext,-1

,null,0

);wchar_t

* pwidechar =

new(std::nothrow)

wchar_t

[isizeinchars];if

(pwidechar ==

null

)wmemset

(pwidechar,

0, isizeinchars)

;multibytetowidechar

(cp_acp,

0, psztext,-1

, pwidechar, isizeinchars)

; std::wstring strresult = std::

wstring

(pwidechar)

;delete

pwidechar;

pwidechar =

null

;return strresult;

}// 寬字元字串轉utf-8字串

inline std::string _w2u8_

(const

wchar_t

* pwsztext)

int isizeinbytes =

widechartomultibyte

(cp_utf8,

0, pwsztext,-1

,null,0

,null

,null);

char

* putf8 =

new(std::nothrow)

char

[isizeinbytes];if

(putf8 ==

null

)memset

(putf8,

0, isizeinbytes)

;widechartomultibyte

(cp_utf8,

0, pwsztext,-1

, putf8, isizeinbytes,

null

,null);

std::string strresult = std::

string

(putf8)

;delete

putf8;

putf8 =

null

;return strresult;

}// 多位元組字串轉utf-8字串

inline std::string _a2u8_

(const

char

* psztext)

#define _u82a(lpu8text) (const_cast(_u82a_(static_cast(lpu8text)).c_str()))

#define _a2u8(lpsztext) (const_cast(_a2u8_(static_cast(lpsztext)).c_str()))

/ utf-8轉本地編碼以及本地編碼轉utf-8 ///

寬位元組UTF 8 多位元組互轉

在進行windows程式設計時,常常遇到不同字元編碼之間的轉換以對應不同的輸出格式,本文介紹寬位元組utf 8編碼格式和多位元組之間的專案轉換。分別呼叫windows底層函式multibytetowidechar和 widechartomultibyte實現。1.utf 8轉多位元組 std str...

多位元組編碼字串與UTF8字元編碼的轉換

1 多位元組編碼字串轉utf8字元編碼 std string csqlitedbmanager to utf8 std string strdata 轉換乙個寬字串到utf8字串 int iutf8len widechartomultibyte cp utf8,0,lpszw,ilen,null,0...

多位元組 unicode和utf 8的轉換

本來在網上能找到很多這方面的 但很多都是 且很多細節讓人理解起來很彆扭,估計有的 的也是不知所云。雖說就兩個windows api的呼叫,但只有自己去寫 測試研究,才真正領會了這些細節。文中注釋有寫的不當的歡迎指正。下面是我寫的測試程式 cpp view plain copy include std...