C++ 常用转换API记录
//wstring转string
std::string wstring2string(IN std::wstring& wstr)
{
std::string result;
//获取缓冲区大小,并申请空间,缓冲区大小事按字节计算的
int len = WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), wstr.size(), NULL, 0, NULL, NULL);
char* buffer = new char[len + 1];
//宽字节编码转换成多字节编码
WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), wstr.size(), buffer, len, NULL, NULL);
buffer[len] = '\0';
//删除缓冲区并返回值
result.append(buffer);
delete[] buffer;
return result;
} //string转wstring
std::wstring string2wstring(IN std::string str)
{
std::wstring result;
//获取缓冲区大小,并申请空间,缓冲区大小按字符计算
int len = MultiByteToWideChar(CP_ACP, 0, str.c_str(), str.size(), NULL, 0);
TCHAR* buffer = new TCHAR[len + 1];
//多字节编码转换成宽字节编码
MultiByteToWideChar(CP_ACP, 0, str.c_str(), str.size(), buffer, len);
buffer[len] = '\0';//添加字符串结尾
//删除缓冲区并返回值
result.append(buffer);
delete[] buffer;
return result;
} //CstringToString
std::string cstring2string(IN CString cstr)
{
//ATL字符串转换宏
std::string str = CT2A(cstr.GetString());
return str;
} //StringToCstring
CString string2cstring(IN std::string str)
{
//ATL字符串转换宏
CString cc = CA2T(str.c_str());
return cc;
} //UTF-8到GB2312的转换
std::string U2G(IN const char* utf8)
{
std::string result;
int len = MultiByteToWideChar(CP_UTF8, 0, utf8, -1, NULL, 0);
wchar_t* wstr = new wchar_t[len + 1];
memset(wstr, 0, len + 1);
MultiByteToWideChar(CP_UTF8, 0, utf8, -1, wstr, len);
len = WideCharToMultiByte(CP_ACP, 0, wstr, -1, NULL, 0, NULL, NULL);
char* str = new char[len + 1];
memset(str, 0, len + 1);
WideCharToMultiByte(CP_ACP, 0, wstr, -1, str, len, NULL, NULL);
result.append(str);
if (wstr) delete[] wstr;
if (str) delete[] str;
return result;
} //GB2312到UTF-8的转换
std::string G2U(IN const char* gb2312)
{
std::string result;
int len = MultiByteToWideChar(CP_ACP, 0, gb2312, -1, NULL, 0);
wchar_t* wstr = new wchar_t[len + 1];
memset(wstr, 0, len + 1);
MultiByteToWideChar(CP_ACP, 0, gb2312, -1, wstr, len);
len = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL);
char* str = new char[len + 1];
memset(str, 0, len + 1);
WideCharToMultiByte(CP_UTF8, 0, wstr, -1, str, len, NULL, NULL);
result.append(str);
if (wstr) delete[] wstr;
if (str) delete[] str;
return result;
} //字符串替换
std::string& replace_str(IN std::string& str, IN const std::string& pattern, IN const std::string& replacestr)
{
for (std::string::size_type pos(0); pos != std::string::npos; pos += replacestr.length())
{
pos = str.find(pattern, pos);
if (pos != std::string::npos)
str.replace(pos, pattern.length(), replacestr);
else
break;
}
return str;
}
参考链接:
https://www.cnblogs.com/babietongtianta/p/3143900.html
https://www.cnblogs.com/lpxblog/p/9855791.html
C++ 常用转换API记录的更多相关文章
- elasticsearch中常用的API
elasticsearch中常用的API分类如下: 文档API: 提供对文档的增删改查操作 搜索API: 提供对文档进行某个字段的查询 索引API: 提供对索引进行操作,查看索引信息等 查看API: ...
- [Windows Phone]常用类库&API推荐
原文 [Windows Phone]常用类库&API推荐 简介: 把自己的应用程序搭建在稳定的API之上,这会使得我们在开发时能把精力都集中在程序的业务逻辑之上,避免重复造轮子,并且使得程序结 ...
- 生活常用类API调用的代码示例合集:邮编查询、今日热门新闻查询、区号查询等
以下示例代码适用于 www.apishop.net 网站下的API,使用本文提及的接口调用代码示例前,您需要先申请相应的API服务. 邮编查询:通过邮编查询地名:通过地名查询邮编 今日热门新闻查询:提 ...
- Java之Object类和常用的API
Object类和常用的API 学习过程中的笔记,涉及到Objetc中的equals方法和toString方法,日期类Date,日历类Calendar,日期格式化类SimpleDateFormat以及基 ...
- ffplay的快捷键以及选项 FFmpeg 基本用法 FFmpeg常用基本命令 ffmpeg常用转换命令,支持WAV转AMR
ffmpeg -i 32_mkv_h264_718x480_ac3.mkv -codec copy -bsf:v h264_mp4toannexb -f mpegts xx.ts ./ffmpeg ...
- Atitit.常用分区api的attilax总结
Atitit.常用分区api的attilax总结 1. Api 来源与oracle与mysql1 1.1. 分区定义partition by range (uid) 使用VALUES LESS TH ...
- Atitit.常用语言的常用内部api 以及API兼容性对源码级别可移植的重要性 总结
Atitit.常用语言的常用内部api 以及API兼容性对源码级别可移植的重要性 总结 1.1. 要兼容的重要语言api1 1.2. 常用基础api分类 core api1 1.3. 比较常用的扩展库 ...
- 常用快递API及快递在线下单API分享
1.常用快递API 支持顺丰.EMS.申通.圆通.韵达.汇通.中通.天天.德邦.全峰等主流快递公司. 文档地址:https://www.juhe.cn/docs/api/id/43 1.1常用快递查询 ...
- 常用Java API之Scanner:功能与使用方法
Scanner 常用Java API之Scanner:功能与使用方法 Scanner类的功能:可以实现键盘输入数据到程序当中. 引用类型的一般使用步骤:(Scanner是引用类型的) 1.导包 imp ...
- python格式转换的记录
Python的格式转换太难了. 与其说是难,具体来说应该是"每次都会忘记该怎么处理".所以于此记录,总的来说是编码+格式转换的记录. 本文记录环境:python3.6 经常见到的格 ...
随机推荐
- JVM实战—3.JVM垃圾回收的算法和全流程
大纲 1.JVM内存中的对象何时会被垃圾回收 2.JVM中的垃圾回收算法及各算法的优劣 3.新生代和老年代的垃圾回收算法 4.避免本应进入S区的对象直接升入老年代 5.Stop the World问题 ...
- 融云技术分享:基于WebRTC的实时音视频首帧显示时间优化实践
本文由融云技术团队原创投稿,作者是融云WebRTC高级工程师苏道,转载请注明出处. 1.引言 在一个典型的IM应用里,使用实时音视频聊天功能时,视频首帧的显示,是一项很重要的用户体验指标. 本文主要通 ...
- KMS for Windows 11
I. 镜像下载 Windows 镜像下载地址:站点1,站点2 II. 手动激活 参考文档:Easy ways to activate Windows 11 for FREE without a pro ...
- 永远不要轻易设置Oracle的隐藏参数,哪怕是DRM
这篇文章可能会存在较大争议,甚至颠覆一些人的固有思维. 因为关于Oracle的隐藏参数,江湖上一直都有两派对立的观点: 1.不要设置任何隐藏参数,只有当遇到特殊问题时在售后指导下临时使用,在问题解决后 ...
- c# Moq Ref/out 参数
public interface IService { void DoSomething(ref string a); void DoSomething2(out string a); } [Test ...
- runoob-数据结构与算法
https://www.runoob.com/data-structures/data-structures-tutorial.html 数据结构(英语:data structure)是计算机中存储. ...
- Solon Cloud Gateway 开发:导引
Solon Cloud Gateway 是 Solon Cloud 体系提供的分布式网关实现(轻量级实现). 分布式网关的特点(相对于本地网关): 提供服务路由能力 提供各种拦截支持 1.分布式网关推 ...
- SQL Server与ORACLE数据库存储过程编写的几个不同之处
一直在使用SQL Server数库的存储过程进行业务数据处理,现在ORACLE上进行存储过程应用,感觉没有MSSQL的方便灵活,总结了以下几点区别: 1.入参数据类型不要书写长度.比如:userNam ...
- 重试机制与 CompletableFuture 拓展
重试机制与 CompletableFuture 拓展 禁止转载. 本文旨在讨论重试机制的特点和策略,分析常用重试类库的实现,讨论为 CompletableFuture 添加重试机制的方法.文章首发同名 ...
- 手把手教你部署 DeepSeek 本地模型
本文目标:部署 DeepSeek 本地模型,并通过 Ollama 提供 API 支持,Chatbox 提供 UI 界面. 原则:不搞那些高深的玩法,让小白也能理解并真正的上手实践. 1.下载Ollam ...