windows API實現中文中字串與GBK、Unicode、UTF-8三種編碼互轉
#include <iostream> #include <string> #include <Windows.h> using namespace std; //gbk轉UTF-8 string GbkToUtf8(const std::string& strGbk)//傳入的strGbk是GBK編碼 { //gbk轉unicode int len = MultiByteToWideChar(CP_ACP, 0, strGbk.c_str(), -1, NULL, 0); wchar_t *strUnicode = new wchar_t[len]; wmemset(strUnicode, 0, len); MultiByteToWideChar(CP_ACP, 0, strGbk.c_str(), -1, strUnicode, len); //unicode轉UTF-8 len = WideCharToMultiByte(CP_UTF8, 0, strUnicode, -1, NULL, 0, NULL, NULL); char * strUtf8 = new char[len]; WideCharToMultiByte(CP_UTF8, 0, strUnicode, -1, strUtf8, len, NULL, NULL); std::string strTemp(strUtf8);//此時的strTemp是UTF-8編碼 delete[]strUnicode; delete[]strUtf8; strUnicode = NULL; strUtf8 = NULL; return strTemp; } //UTF-8轉gbk string Utf8ToGbk(const std::string& strUtf8)//傳入的strUtf8是UTF-8編碼 { //UTF-8轉unicode int len = MultiByteToWideChar(CP_UTF8, 0, strUtf8.c_str(), -1, NULL, 0); wchar_t * strUnicode = new wchar_t[len];//len = 2 wmemset(strUnicode, 0, len); MultiByteToWideChar(CP_UTF8, 0, strUtf8.c_str(), -1, strUnicode, len); //unicode轉gbk len = WideCharToMultiByte(CP_ACP, 0, strUnicode, -1, NULL, 0, NULL, NULL); char *strGbk = new char[len];//len=3 本來為2,但是char*後面自動加上了\0 memset(strGbk, 0, len); WideCharToMultiByte(CP_ACP,0, strUnicode, -1, strGbk, len, NULL, NULL); std::string strTemp(strGbk);//此時的strTemp是GBK編碼 delete[]strUnicode; delete[]strGbk; strUnicode = NULL; strGbk = NULL; return strTemp; } //gbk轉unicode (下面的例子沒用到) wstring GbkToUnicode(const std::string& strGbk)//返回值是wstring { int len = MultiByteToWideChar(CP_ACP, 0, strGbk.c_str(), -1, NULL, 0); wchar_t *strUnicode = new wchar_t[len]; wmemset(strUnicode, 0, len); MultiByteToWideChar(CP_ACP, 0, strGbk.c_str(), -1, strUnicode, len); std::wstring strTemp(strUnicode);//此時的strTemp是Unicode編碼 delete[]strUnicode; strUnicode = NULL; return strTemp; } //Unicode轉gbk string UnicodeToGbk (const std::wstring& strUnicode)//引數是wstring { int len = WideCharToMultiByte(CP_ACP, 0, strUnicode.c_str(), -1, NULL, 0, NULL, NULL); char *strGbk = new char[len];//len=3 本來為2,但是char*後面自動加上了\0 memset(strGbk, 0, len); WideCharToMultiByte(CP_ACP,0,strUnicode.c_str(), -1, strGbk, len, NULL, NULL); std::string strTemp(strGbk);//此時的strTemp是GBK編碼 delete[]strGbk; strGbk = NULL; return strTemp; } int main() { //1、ANSI/GBK編碼 string strGbk = "我"; int num = strGbk.size();//獲取兩個字元數,也是我字所佔的位元組數 unsigned char* p = (unsigned char*)strGbk.c_str(); for (int i = 0; i < num; i++) { printf("%0x", *p); p++; } //輸出ced2 所以我的GBK編碼是0xced2 printf("\n"); char gbk[] = {0xce, 0xd2, 0x00}; //加上0x00字串結束符,不會輸出亂碼 cout<<gbk<<endl;//輸出漢字我 //2、unicodde編碼 //方法一 //wchar_t str = 0x6211; //wcout.imbue(locale("chs")); //wcout << str << endl;//輸出漢字我 //wchar_t c=L'我'; //cout << hex << (short)c << endl<<endl;//輸出unicodde編碼 6211 //方法二: wstring strUnicode = L"我";//轉成unicode編碼 num = strUnicode.size()*2;//乘以2,才是我所佔的位元組數 p = (unsigned char*)strUnicode.c_str(); for (int i = 0; i < num; i++) { printf("%0x", *p); p++; } //輸出1162 因為預設是小端模式,所以我的unicode編碼是0x6211 printf("\n"); wchar_t s[2] = {0x6211, 0x00}; //加上0x00字串結束符,不會輸出亂碼 wstring str =(wchar_t*)s; cout<<UnicodeToGbk(str)<<endl;//需要先將unicode字串轉成gbk之後才能用cout輸出 //3、UTF-8編碼 string strUtf8 = GbkToUtf8("我");//轉成utf8編碼 num = strUtf8.size();//num=3 p = (unsigned char*)strUtf8.c_str(); for (int i = 0; i < num; i++) { printf("%0x", *p); p++; } //輸出e68891 printf("\n"); char utf8[] = {0xe6, 0x88, 0x91,0x00}; //加上0x00字串結束符,不會輸出亂碼 cout<<Utf8ToGbk(utf8)<<endl;//需要先將utf8字串轉成gbk之後才能用cout輸出 return 0; }
相關推薦
windows API實現中文中字串與GBK、Unicode、UTF-8三種編碼互轉
#include <iostream> #include <string> #include <Windows.h> using namespace std; //gbk轉UTF-8 string GbkToUtf8(const std
利用純c++和windows api實現gb2312和utf-8兩種編碼格式的轉換
為什麼同樣的字串在兩臺主機上,會出現一個顯示正常,一個顯示亂碼的情況呢? 答案:編碼方式不匹配。 解釋:任何內容在計算機中的儲存形式都是二進位制,不論是在記憶體中還是在硬碟中。所以,同一個字串在兩臺主機上的二進位制儲存是一模一樣的。只是將這個二進位制資料呈現時,發生了變化。呈現字串的過程就是
【轉載】字元編碼中ASCII、Unicode和UTF-8的區別
1. ASCII碼 我們知道,在計算機內部,所有的資訊最終都表示為一個二進位制的字串。每一個二進位制位(bit)有0和1兩種狀態,因此八個二進位制位就可以組合出256種狀態,這被稱為一個位元組(byte)。也就是說,一個位元組一共可以用來表示256種不同的狀態,每一個狀態對應一個符
MFC中CString類字串與長整型、浮點型、字元陣列char資料之間的相互轉換
一、長整型資料與CString類字串相互轉換 1.將長整型資料轉換為CString字串類 CString str; long ld; str.Format(_T("%ld"),ld); 2.將CString字串類轉換為長整型資料 CString str; long ld; ld=
轉 c#中 base64字串與普通字串互轉
https://blog.csdn.net/hwt0101/article/details/79758912 轉成 Base64 形式的 System.String: string a = "base64字串與普通字串互轉"; by
Python中raw字串與多行字串
如果一個字串包含很多需要轉義的字元,對每一個字元都進行轉義會很麻煩。為了避免這種情況,我們可以在字串前面加個字首 r ,表示這是一個 raw 字串,裡面的字元就不需要轉義了。例如: r'\(~_~)/
MySQLQ中的字串與日期格式的轉換(精講)
str_to_date函式:將字串轉換為日期 作用:將‘日期字串’轉換為‘日期型別’資料 執行結果:DATE型別 用法:str_to_date(‘日期字串’,‘日期格式’) MySQL日期格式: %Y:代表4位的年份 %y:代表2位的年份 %m:代
windows批處理中的字串處理詳解
轉載:https://www.jb51.net/article/52744.htm 1、擷取字串 擷取字串可以說是字串處理功能中最常用的一個子功能了,能夠實現擷取字串中的特定位置的一個或多個字元。舉例說明其基本功能: 程式碼如下: @echo off set ifo=abcd
c#中 base64字串與普通字串互轉(圖片byte 跨平臺傳輸的坑)
在unity跟 安卓ios互動的時候 除了可以傳輸 string int型別的之外 還是可以傳輸byte[]型別的 安卓的java比較簡單,ios的oc就比較難受了,所以用到base64轉換的話就不用考慮那麼多了。所有傳到unity的 都轉換為string型別就可以,我們只需
Windows API實現檔案操作
下面是windows中提供的對於檔案進行操作的API函式及其功能: Windows API函式 功能 CloseHandle 關閉一個核心物件。其中包括檔案、檔案對映、程序、執行緒、安全和同步物件等。涉及檔案處理時,這個函式通常與vb的close命令相似。應儘可能的使
1-6 Python中raw字串與多行字串
如果一個字串包含很多需要轉義的字元,對每一個字元都進行轉義會很麻煩。為了避免這種情況,我們可以在字串前面加個字首r ,表示這是一個 raw 字串,裡面的字元就不需要轉義了。例如: r'\(~_~)/ \(~_~)/' 但是r'...'表示法不能表示多行字串,也不
C和C++中的字串與數字轉換函式
前言: 今天開始想要好好補補程式,開始看老早就買了的《演算法入門經典》,發現前面幾章對字串的處理較多,蒐羅了一下別人的部落格,整理到這上面來。 C語言中常用的字串和數字轉換函
Python中的字串與字元編碼
本節內容: 前言 相關概念 Python中的預設編碼 Python2與Python3中對字串的支援 字元編碼轉換 一、前言 Python中的字元編碼是個老生常談的話題,同行們都寫過很多這方面的文章。有的人云亦云,也有的寫得很深入。近日看到某知名培訓機構的教學視訊中再次談及此問題,講解的還是不盡人意,所
Android中關於字串與顏色的轉換問題
最近遇到後臺傳遞顏色字串,然後根據傳遞的字串顏色改變TextView的背景顏色的問題。 解決方案: TextView mTextView= (TextView) view.findViewById
windows批處理中的%errorlevel%與!errorlevel!
bat指令碼中常用%errorlevel%表達上一條命令的返回值,用於判斷。比如: cmd1 if %errorlevel% == 1 ( cmd2 ) //如果cmd1返回的錯誤碼值等於1時,將執行cmd2操作 一般上一條命令的執行結果返回的值
windows api實現計算機空閒關機
1.實現監聽全域性的滑鼠和鍵盤事件,2分鐘不動則關機 2.實現計算機基本資訊和效能資訊的獲取,比如C盤利用率,CPU利用率,記憶體利用率等等 3.開闢新的執行緒 完整程式碼如下: #include <windows.h> #include <time.h&
Python中的字串與字元編碼:編碼和轉換問題
本節內容: 前言相關概念Python中的預設編碼Python2與Python3中對字串的支援字元編碼轉換 一、前言 Python中的字元編碼是個老生常談的話題,同行們都寫過很多這方面的文章。有的人云亦云,也有的寫得很深入。近日看到某知名培訓機構的教學視訊中再次談及此問題,講解的還是不盡人意,所以才想寫這篇
使用Windows API實現執行時動態獲取程式檔案版本資訊
檔案版本資訊的存在使得應用程式正確的安裝檔案變得簡單,並且使安裝程式能夠分析檔案的當前安裝狀態。通常,版本資訊應該包括檔案的版本號、檔案功能描述以及檔案作者等多項重要內容。 在應用層面上,程式設計師可以通過儲存在應用程式檔案或動態連結庫檔案中的版本資訊判斷一個檔案是否
windows api實現視窗、按鈕、靜態文字框透明顯示
1.需求(1)實現一個無邊框的視窗,不需要視窗能夠拖動,不需要放大縮小按鈕,但是需要右上角的關閉按鈕;(2)靜態文字框背景透明,顯示的文字能夠頻繁重新整理並且不能夠有重影;2.實現方式2.1無邊框視窗實現去除邊框是在視窗過程函式的WM_SIZE訊息處理時實現的。case WM