一、中文字符串
- 問題來源:
中文字符長度不固定,按字節處理往往出現亂碼或錯誤分割。在unicode中每個中文爲2個字節,而中文中間夾雜的英文和半角標點則仍然是1個字節。 - 解決方案:
構造三層邏輯結構:輸入層、邏輯處理層、輸出層。
– 輸入層接收char輸入,並將其轉換爲wchar.
– 邏輯處理層在 wchar* 或 wstring 的基礎上進行字符串操作,此時操作最小單位爲中文字符,不會再有亂碼。
– 輸出層將wchar的結果再次轉換爲char ,返回給外部。
這樣,對外部來說,仍然是輸入char*, 輸出char*, 但在這個過程中不再有分割漢字的操作或亂碼。
#include <iostream>
#include <string>
using namespace std;
wchar_t* MBCS2Unicode(wchar_t * buff, const char * str)
{
wchar_t * wp = buff;
char * p = (char *)str;
while (*p)
{
if (*p & 0x80)
{
*wp = *(wchar_t *)p;
p++;
}
else {
*wp = (wchar_t)*p;
}
wp++;
p++;
}
*wp = 0x0000;
return buff;
}
char * Unicode2MBCS(char * buff, const wchar_t * str)
{
wchar_t * wp = (wchar_t *)str;
char * p = buff, *tmp;
while (*wp) {
tmp = (char *)wp;
if (*wp & 0xFF00) {
*p = *tmp;
p++; tmp++;
*p = *tmp;
p++;
}
else {
*p = *tmp;
p++;
}
wp++;
}
*p = 0x00;
return buff;
}
wstring str2wstr(string str)
{
size_t len = str.size();
wchar_t * b = (wchar_t *)malloc((len + 1) * sizeof(wchar_t));
MBCS2Unicode(b, str.c_str());
wstring r(b);
free(b);
return r;
}
int wputs(const wchar_t * wstr);
int wputs(wstring wstr)
{
wputs(wstr.c_str());
return 0;
}
int wputs(const wchar_t * wstr)
{
int len = wcslen(wstr);
char * buff = (char *)malloc((len * 2 + 1) * sizeof(char));
Unicode2MBCS(buff, wstr);
printf("%s", buff);
free(buff);
return 0;
}
string wstr2str(wstring wstr)
{
size_t len = wstr.size();
char * b = (char *)malloc((2 * len + 1) * sizeof(char));
Unicode2MBCS(b, wstr.c_str());
string r(b);
free(b);
return r;
int main()
{
//輸入層:接收char*輸入,並將其轉換爲wchar*
string input = "今天你在武漢!!";
string temp = "在";
//邏輯層在whcar*或wstring的基礎上進行字符串操作,此時操作最小單位爲中文字符,不會再有亂碼。
wstring buff = str2wstr(input);
wstring temp_buff = str2wstr(temp);
cout << "input的中文個數:"<<wcslen(buff.c_str()) << " temp的中文個數:" << wcslen(temp_buff.c_str()) << endl;
cout << "判斷input第三個是否是'在':" << (buff[3] == temp_buff[0]) << endl;;
//輸出層將wchar*的結果再次轉換爲char*,返回給外部
wputs(buff);
return 0;
}
二、c++幾種編碼之前的轉換
.h
#pragma once
#include "stdafx.h"
#include "windows.h"
#include <iostream>
#include <string>
using namespace std;
//**************string******************//
// ASCII與Unicode互轉
wstring AsciiToUnicode(const string& str);
string UnicodeToAscii(const wstring& wstr);
// UTF8與Unicode互轉
wstring Utf8ToUnicode(const string& str);
string UnicodeToUtf8(const wstring& wstr);
// ASCII與UTF8互轉
string AsciiToUtf8(const string& str);
string Utf8ToAscii(const string& str);
//**************CString******************//
// ASCII與Unicode互轉
CStringW AsciiToUnicode_CSTR(const CStringA& str);
CStringA UnicodeToAscii_CSTR(const CStringW& wstr);
// UTF8與Unicode互轉
CStringW Utf8ToUnicode_CSTR(const CStringA& str);
CStringA UnicodeToUtf8_CSTR(const CStringW& wstr);
// ASCII與UTF8互轉
CStringA AsciiToUtf8_CSTR(const CStringA& str);
CStringA Utf8ToAscii_CSTR(const CStringA& str);
/************string-int***************/
// string 轉 Int
int StringToInt(const string& str);
string IntToString(int i);
string IntToString(char i);
string IntToString(double i);
.cpp
#pragma once
#include "stdafx.h"
#include "UTN.h"
wstring AsciiToUnicode(const string& str) {
// 預算-緩衝區中寬字節的長度
int unicodeLen = MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, nullptr, 0);
// 給指向緩衝區的指針變量分配內存
wchar_t *pUnicode = (wchar_t*)malloc(sizeof(wchar_t)*unicodeLen);
// 開始向緩衝區轉換字節
MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, pUnicode, unicodeLen);
wstring ret_str = pUnicode;
free(pUnicode);
return ret_str;
}
string UnicodeToAscii(const wstring& wstr) {
// 預算-緩衝區中多字節的長度
int ansiiLen = WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), -1, nullptr, 0, nullptr, nullptr);
// 給指向緩衝區的指針變量分配內存
char *pAssii = (char*)malloc(sizeof(char)*ansiiLen);
// 開始向緩衝區轉換字節
WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), -1, pAssii, ansiiLen, nullptr, nullptr);
string ret_str = pAssii;
free(pAssii);
return ret_str;
}
wstring Utf8ToUnicode(const string& str) {
// 預算-緩衝區中寬字節的長度
int unicodeLen = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, nullptr, 0);
// 給指向緩衝區的指針變量分配內存
wchar_t *pUnicode = (wchar_t*)malloc(sizeof(wchar_t)*unicodeLen);
// 開始向緩衝區轉換字節
MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, pUnicode, unicodeLen);
wstring ret_str = pUnicode;
free(pUnicode);
return ret_str;
}
string UnicodeToUtf8(const wstring& wstr) {
// 預算-緩衝區中多字節的長度
int ansiiLen = WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), -1, nullptr, 0, nullptr, nullptr);
// 給指向緩衝區的指針變量分配內存
char *pAssii = (char*)malloc(sizeof(char)*ansiiLen);
// 開始向緩衝區轉換字節
WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), -1, pAssii, ansiiLen, nullptr, nullptr);
string ret_str = pAssii;
free(pAssii);
return ret_str;
}
string AsciiToUtf8(const string& str) {
return UnicodeToUtf8(AsciiToUnicode(str));
}
string Utf8ToAscii(const string& str) {
return UnicodeToAscii(Utf8ToUnicode(str));
}
// ASCII與Unicode互轉
CStringW AsciiToUnicode_CSTR(const CStringA& str) {
return AsciiToUnicode(LPCSTR(str)).c_str();
}
CStringA UnicodeToAscii_CSTR(const CStringW& wstr) {
return UnicodeToAscii(LPCWSTR(wstr)).c_str();
}
// UTF8與Unicode互轉
CStringW Utf8ToUnicode_CSTR(const CStringA& str) {
return Utf8ToUnicode(LPCSTR(str)).c_str();
}
CStringA UnicodeToUtf8_CSTR(const CStringW& wstr) {
return UnicodeToUtf8(LPCWSTR(wstr)).c_str();
}
// ASCII與UTF8互轉
CStringA AsciiToUtf8_CSTR(const CStringA& str) {
return UnicodeToUtf8_CSTR(AsciiToUnicode_CSTR(str));
}
CStringA Utf8ToAscii_CSTR(const CStringA& str) {
return UnicodeToAscii_CSTR(Utf8ToUnicode_CSTR(str));
}
// string 與 Int 互轉
int StringToInt(const string& str) {
return atoi(str.c_str());
}
string IntToString(int i) {
char ch[1024];
memset(ch, 0, 1024);
sprintf_s(ch, sizeof(ch), "%d", i);
return ch;
}
string IntToString(char i) {
char ch[1024];
memset(ch, 0, 1024);
sprintf_s(ch, sizeof(ch), "%c", i);
return ch;
}
string IntToString(double i) {
char ch[1024];
memset(ch, 0, 1024);
sprintf_s(ch, sizeof(ch), "%f", i);
return ch;
}