wstring_convert

2024-09-04 22:18
文章标签 convert wstring

本文主要是介绍wstring_convert,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

char*和wchar_t*之间的相互转换。

W2A及A2W

要点:
- USES_CONVERSION;
- W2A及A2W
- 只能用在c++代码中。

代码:
#include

wstring_convert

参考: http://www.cplusplus.com/reference/locale/wstring_convert/

最新的C++编译器才行。

该链接中的代码示例:

// converting from UTF-32 to UTF-8
#include <iostream>       // std::cout, std::hex
#include <string>         // std::string, std::u32string
#include <locale>         // std::wstring_convert
#include <codecvt>        // std::codecvt_utf8
#include <cstdint>        // std::uint_least32_tint main ()
{std::u32string str32 ( U"\U00004f60\U0000597d" );  // ni hao (你好)std::string str8;std::wstring_convert<std::codecvt_utf8<char32_t>,char32_t> cv;str8 = cv.to_bytes(str32);// print contents (as their hex representations):std::cout << std::hex;std::cout << "UTF-32: ";for (char32_t c : str32)std::cout << '[' << std::uint_least32_t(c) << ']';std::cout << '\n';std::cout << "UTF-8 : ";for (char c : str8)std::cout << '[' << int(static_cast<unsigned char>(c)) << ']';std::cout << '\n';return 0;
}

WideCharToMultiByte()

示例转换函数:

bool ws2s(const std::wstring& ws, std::string& s)
{const wchar_t* pws = ws.c_str();int iByte = WideCharToMultiByte(CP_OEMCP, 0, pws, -1, NULL, 0, NULL, NULL);if (iByte == 0) return false;char* ps;ps = (char*)malloc(iByte);if (NULL == ps) return false;WideCharToMultiByte(CP_OEMCP, 0, pws, -1, ps, iByte, NULL, NULL);s = std::string(ps);free(ps);return true;
}bool s2ws(const std::string& s, std::wstring& ws)
{const char* ps = s.c_str();int iByte = ::MultiByteToWideChar(CP_OEMCP, 0, ps, -1, NULL, 0);if (iByte == 0) return false;wchar_t* pws;pws = (wchar_t*)malloc(iByte * sizeof(wchar_t));if (NULL == pws) return false;::MultiByteToWideChar(CP_OEMCP, 0, ps, -1, pws, iByte);ws = std::wstring(pws);free(pws);return true;
}

这篇关于wstring_convert的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1137179

相关文章

org.springframework.core.convert.ConversionFailedException

适用: 1.你在springmvc.xml中写了静态资源处理<mvc:resources location="/" mapping="/**"></mvc:resources> 2.你在springmvc.xml中写了日期类型转换 解决方法: 首先检测你的spring版本,然后 1.spring3.0后将<mvc:resources location="/" mapping="/**">

ValueError: could not convert string ‘date‘ to float64 at row 0, column 1.

ValueError:无法将字符串“date”转换为第0行第1列的float64。 解决办法: 通过以下代码检查自己的数据,查找问题,逐一改正。 import csvdef find_blank_or_invalid_cells(file_path):blank_cells = []invalid_cells = []with open(file_path, 'r', newline=

ptr.inl.hpp:121:13: error: cannot convert ‘const char*’ to ‘cv::Formatted*’ in initialization

opencv 里面定义了 print()函数 。如果在写C++时候,将printf 写成了print就会报这个错误。

leetcode 刷题之路 14 Convert Sorted Array to Binary Search Tree

Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 题目要求将有序数组转换为一个二元查找树。树的题目大部分都可以采用递归来解决,这道题也不例外。一个二元查找树的左子树上的所有节点都小于根节点,右子树上的所有节点都大于根节点,同时二元查找树左子树和右子树上

Leetcode143: Convert Sorted List to Binary Search Tree

Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. 与排序好的数组转化为二分搜索树的题相似,可以先把链表转化为数组在转化为树。 /*** Definition for singly-linked list.* struct

PPT到PDF转换器:一个功能强大的Python GUI应用(unzip,convert,replace,merge)

在当今的数字时代,文档格式转换已成为一项常见需求。特别是将PowerPoint演示文稿转换为PDF格式,这不仅可以确保文档的一致性,还能方便分享和打印。今天,我们将深入探讨一个使用Python开发的强大GUI应用程序,它不仅可以将PPT转换为PDF,还具备文本替换、PDF处理和文件合并等多项功能。 C:\pythoncode\new\unzipconvertpdfmerge.py 应用概述 这

PIL convert(‘RGB‘) 用法

PIL 不提供 BGR转RGB的方法。 1. 图像模式转换 如果图像当前的模式不是 RGB,例如它是灰度(L)、CMYK 或其他模式时,convert('RGB') 会将图像转换为 RGB 格式。 灰度图像 (L): 如果图像是灰度图像 (L),convert('RGB') 会将其转换为 RGB 模式。每个灰度值会复制到红、绿、蓝三个通道上。 img = Image.open("gra

SQL Server 查询语句中,对索引列做CONVERT的影响

通常,在做SQL Server查询语句优化的时候,如果发现语句对索引列做了函数计算,都会建议改写,将计算的逻辑转移到筛选条件列上。但这种对索引列的计算,有时却会带来一些额外的好处。请看以下的例子: --测试数据库 adventureworks2022,兼容级别160--创建如下的索引:USE AdventureWorks2022goCREATE NONCLUSTERED

使用try-convert将.NET Framework项目迁移到.NET Core

try-convert 是一个命令行工具,它可以帮助开发者将 .NET Framework 项目迁移到 .NET Core 或 .NET 5/6/7(以及更高版本,取决于发布时的最新版本)。这是 Microsoft 官方提供的一个工具,旨在简化迁移过程,但请注意,它不会自动解决所有兼容性问题,因为它主要是帮助进行项目文件和配置文件的转换。 以下是将 .NET Framework 项目迁移到 .N

Failed to convert property value of type ‘java.lang.String‘ to required type ‘java.util.Date‘

spring boot的日期转换问题 前言解决方法原因 前言 小编的springboot项目已经配置了全局的日期转换,并且在项目中日期自动上添加了 @JsonFormat(pattern = “yyyy-MM-dd HH-mm-ss”) 的日期转换注解,但是今天遇到一个奇怪的问题,部分日期的转换还是报错了。 解决方法 在日期的注解上在添加一个日期装的注解。DateTimeF