当前位置: 首页 > news >正文

10 编码转换问题

文章目录

  • 字符编码问题
  • 编码转换问题
    • ANSI转Unicode
    • Unicode转ANSI
    • Utf8转 ANSI
    • utf8 转Unicode
    • ANSI 转UTF-8
    • Unicode 转 UTF-8
  • 全部代码

字符编码问题

在这里插入图片描述

Windows API 函数
MessageBoxA:MessageBox 内部实现,字符串编码(ANSI)转换成了Unicode,在调用MessageboxW
MessageBox:是一个宏定义
MessageBoxA(NULL,"Hello","提示",MB_OK);
MessageBoxW(NULL,L"Hello",L"提示",MB_OK);

编码转换问题

ANSI转Unicode

//ANSI转Unicode
wchar_t* CCharset::AnsiToUnicode(const char* str)
{if (m_wstr)//安全{delete m_wstr;m_wstr = NULL;}DWORD dwSize=::MultiByteToWideChar(CP_ACP,0,str,-1,NULL,0);//求宽字符的大小m_wstr = new wchar_t[dwSize];::MultiByteToWideChar(CP_ACP, 0, str, -1, m_wstr, dwSize);return m_wstr;
}

Unicode转ANSI

//Unicode转ANSI
char * CCharset::UnicodeToAnsi(const wchar_t * wstr)
{if (m_str){delete m_str;m_str = NULL;}DWORD dwSize=WideCharToMultiByte(CP_ACP, 0, wstr, -1,NULL,0,NULL,NULL);m_str = new char[dwSize];::WideCharToMultiByte(CP_ACP, 0, wstr, -1, m_str, dwSize, NULL, NULL);return m_str;
}

Utf8转 ANSI

char * CCharset::Utf8ToAnsi(const char * str)
{if (m_wstr){delete[] m_wstr;m_wstr = NULL;}if (m_str){delete[] m_str;m_str = NULL;}//UTF-8 转Unicodem_wstr= Utf8ToUnicode(str);//Unicode 转ANSIm_str= UnicodeToAnsi(m_wstr);return m_str;
}

utf8 转Unicode

wchar_t* CCharset::Utf8ToUnicode(const char * str)
{if (m_wstr){delete m_wstr;m_wstr = NULL;}DWORD dwSize=MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0);m_wstr = new wchar_t[dwSize];memset(m_wstr, 0, dwSize*sizeof(wchar_t));//清空内存MultiByteToWideChar(CP_UTF8, 0, str, -1, m_wstr, dwSize);return m_wstr;
}

ANSI 转UTF-8

char* CCharset::AnsitoUtf8(const char* str)
{if (m_wstr){delete[] m_wstr;m_wstr = NULL;}if (m_utf8){delete[] m_utf8;m_utf8 = NULL;}//Ansi 转Unicodem_wstr= AnsiToUnicode(str);//Unicode 转UTF-8m_utf8=UnicodeToUtf8(m_wstr);return m_utf8;
}

Unicode 转 UTF-8

char * CCharset::UnicodeToUtf8(const wchar_t * wstr)
{if (m_utf8){delete[] m_utf8;m_utf8 = NULL;}DWORD dwSize=WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL);m_utf8 = new char[dwSize];memset(m_utf8,0,dwSize);//清空内存WideCharToMultiByte(CP_UTF8, 0, wstr, -1, m_utf8, dwSize, NULL, NULL);return m_utf8;
}

全部代码

CCharset.h

#pragma once
class CCharset
{private:wchar_t* m_wstr;char* m_str;char* m_utf8;
public:CCharset();~CCharset();//ANSI转Unicodewchar_t* AnsiToUnicode(const char* str);//Unicode转ANSIchar* UnicodeToAnsi(const wchar_t* wstr);//UTF8 转ANSIchar* Utf8ToAnsi(const char* str);//ANSI转UTF - 8char* AnsitoUtf8(const char* str);//Unicode 转 UTF-8char* UnicodeToUtf8(const wchar_t* wstr);//UTF-8转Unicodewchar_t* Utf8ToUnicode(const char* str);};

CCharset.cpp

#include "pch.h"
#include "CCharset.h"CCharset::CCharset()
{m_wstr = NULL;m_str = NULL;m_utf8 = NULL;
}CCharset::~CCharset()
{if (m_wstr){delete m_wstr;m_wstr = NULL;}if (m_str){delete m_str;m_str = NULL;}if (m_utf8){delete[] m_utf8;m_utf8 = NULL;}
}//ANSI转Unicode
wchar_t* CCharset::AnsiToUnicode(const char* str)
{if (m_wstr)//安全{delete[] m_wstr;m_wstr = NULL;}DWORD dwSize=::MultiByteToWideChar(CP_ACP,0,str,-1,NULL,0);//求宽字符的大小m_wstr = new wchar_t[dwSize];::MultiByteToWideChar(CP_ACP, 0, str, -1, m_wstr, dwSize);return m_wstr;
}//Unicode转ANSI
char * CCharset::UnicodeToAnsi(const wchar_t * wstr)
{if (m_str){delete[] m_str;m_str = NULL;}DWORD dwSize=WideCharToMultiByte(CP_ACP, 0, wstr, -1,NULL,0,NULL,NULL);//求字符的大小m_str = new char[dwSize];::WideCharToMultiByte(CP_ACP, 0, wstr, -1, m_str, dwSize, NULL, NULL);return m_str;
}char * CCharset::Utf8ToAnsi(const char * str)
{if (m_wstr){delete[] m_wstr;m_wstr = NULL;}if (m_str){delete[] m_str;m_str = NULL;}//UTF-8 转Unicodem_wstr= Utf8ToUnicode(str);//Unicode 转ANSIm_str= UnicodeToAnsi(m_wstr);return m_str;
}char* CCharset::AnsitoUtf8(const char* str)
{if (m_wstr){delete[] m_wstr;m_wstr = NULL;}if (m_utf8){delete[] m_utf8;m_utf8 = NULL;}//Ansi 转Unicodem_wstr= AnsiToUnicode(str);//Unicode 转UTF-8m_utf8=UnicodeToUtf8(m_wstr);return m_utf8;
}//Unicode 转 UTF-8
char * CCharset::UnicodeToUtf8(const wchar_t * wstr)
{if (m_utf8){delete[] m_utf8;m_utf8 = NULL;}DWORD dwSize=WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL);m_utf8 = new char[dwSize];memset(m_utf8,0,dwSize);//清空内存WideCharToMultiByte(CP_UTF8, 0, wstr, -1, m_utf8, dwSize, NULL, NULL);return m_utf8;
}//utf8 转Unicode
wchar_t* CCharset::Utf8ToUnicode(const char * str)
{if (m_wstr){delete m_wstr;m_wstr = NULL;}DWORD dwSize=MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0);m_wstr = new wchar_t[dwSize];memset(m_wstr, 0, dwSize*sizeof(wchar_t));//清空内存MultiByteToWideChar(CP_UTF8, 0, str, -1, m_wstr, dwSize);return m_wstr;
}
http://www.lryc.cn/news/94073.html

相关文章:

  • Spring MVC获取参数和自定义参数类型转换器及编码过滤器
  • 理想的实验
  • nginx配置开机启动(Windows环境)
  • MySQL 基础面试题02(事务索引)
  • 主从架构lua脚本-Redis(四)
  • maven与idea版本适配问题
  • ChatGPT扫盲知识库
  • chatgpt赋能python:Python轨迹可视化:用数据讲故事
  • K-means
  • 归并排序(基础+提升)
  • MATLAB应用
  • LeetCode --- 1784. Check if Binary String Has at Most One Segment of Ones 解题报告
  • js:javascript中的事件体系:常见事件、事件监听、事件移除、事件冒泡、事件捕获、事件委托、阻止事件
  • 【数据结构】特殊矩阵的压缩存储
  • 在layui中使用vue,使用vue进行页面数据部分数据更新
  • Vue中如何进行数据导入与Excel导入
  • git 的基本操作
  • 搭建Vue项目以及项目的常见知识
  • TypeScript ~ TS Webpack构建工具 ⑦
  • Rust 自建HTTP Server支持图片响应
  • [游戏开发][Unity]UnityWebRequest使用大全
  • 如何使用Fiddler对手机进行弱网测试?(干货教程)
  • 专业科普:什么是单片机?
  • 深度学习-第T11周——优化器对比实验
  • 基于Dlib的疲劳检测系统
  • three.js通过CubeTexture加载环境贴图,和RGBELoader加载器加载hdr环境贴图
  • pycharm中Terminal输入sqlite3,出现无法将sqlite项识别为cmdlet**的解决方法
  • VSCode 安装配置教程详解包含c++环境配置方法
  • Baumer工业相机堡盟工业相机如何通过BGAPISDK将图像放大缩小显示(C#)
  • 8.1 PowerBI系列之DAX函数专题-进阶-解决列排序对计算的影响