当前位置:网站首页>[character set 6] wide string and multi byte character conversion
[character set 6] wide string and multi byte character conversion
2022-06-12 08:52:00 【junxuezheng】
Wide strings and multibyte characters are converted to each other
This paper mainly focuses on the conversion of wide characters and multi byte characters . In fact, it also involves the conversion of character sets , For your understanding , We will not introduce more here .
One 、windows
- Convert wide characters to multibytes
#include <iostream>
#include <windows.h>
using namespace std;
int main()
{
wchar_t wideChar[] = L" I am a wide character ";
// 1. First, specify the pointer to receive the conversion result
char * multiByte;
// 2. Then call the function for the first time , Get the size of the buffer needed to store the conversion results
int len = WideCharToMultiByte(CP_ACP, NULL,
wideChar, // The wide character string to be converted
-1, // The length to convert , Set to -1 Represents the conversion of the whole string
NULL, // First call , The receive buffer is set to NULL
NULL, // The size of the receive buffer , Set to NULL Indicates that the function returns the required size
NULL, NULL);
// 3. Create a buffer of appropriate size based on the returned results
multiByte = new char[len];
// 4. The second call to the function , Make a real conversion
WideCharToMultiByte(CP_ACP, NULL,
wideChar, // The wide character string to be converted
-1, // The length to convert , Set to -1 Represents the conversion of the whole string
multiByte, // Second call , Set as the buffer pointer to receive the conversion result
len, // Set the receive buffer size
NULL, NULL);
// 5. End of conversion , You can output and view the conversion results
cout << multiByte << endl;
delete multiByte;
system("pause");
return 0;
}
Output
I am a wide character
Please press any key to continue . . .
- Convert multiple bytes to wide characters :
#include <iostream>
#include <windows.h>
using namespace std;
int main()
{
setlocale(LC_ALL, "chs");
char multiByte[] = " I am a multibyte string .";
// 1. First, specify the pointer to receive the conversion result
WCHAR * wideChar;
// 2. Then call the function for the first time , Get the size of the buffer needed to store the conversion results
int len = MultiByteToWideChar(CP_ACP, NULL,
multiByte, // Multibyte string to be converted
-1, // The length to convert , Set to -1 Represents the conversion of the whole string
NULL, // First call , The receive buffer is set to NULL
0); // Receive buffer length , Set to 0 Represents the required length returned by the function
// 3. Create a buffer of appropriate size based on the returned results
wideChar = new WCHAR[len];
// 4. The second call to the function , Make a real conversion
MultiByteToWideChar(CP_ACP, NULL,
multiByte, // Multibyte string to be converted
-1, // The length to convert , Set to -1 Represents the conversion of the whole string
wideChar, // Second call , Set as the buffer to receive the conversion results
len); // Set the size of the receive buffer
// 5. End of conversion , You can output and view the conversion results
wcout << wideChar << endl;
delete wideChar;
system("pause");
return 0;
}
Output
I am a multibyte string .
See : Conversion method between multibyte and wide character
Two 、linux
- iconv.h
- linux Prototype function under
- It can not only convert wide characters and multi byte characters , It can also be used to convert different character codes .
This one doesn't use much , and c++20 The corresponding function conversion is provided , Don't do too much research here , I will sort out an article later c++ 20 Provided character conversion function .
See :linux Conversion between wide characters and multi byte characters
3、 ... and 、c Language
c Language provides wcstombs、mbstowcs( unsafe )
( Because it's not safe , stay vs2017 Can't compile , Let's use safer wcstombs_s、mbstowcs_s, Want to compile through , Can be in visual in : Item right click – attribute – C/C++ – The preprocessor – Preprocessor definition , Add... To it _CRT_SECURE_NO_DEPRECATE that will do )
windows and linux You can use ;
There is 《 Four 、wchar_t》 The problems mentioned in the remarks :【 Character set II 】 Multibyte characters vs Wide characters
wcstombs_s、mbstowcs_s It's from Microsoft ,linux It's definitely not usable .
- wcstombs
size_t wcstombs (char* dest, const wchar_t* src, size_t max);
Convert wide-character string to multibyte string
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#define BUFFER_SIZE 50
int main()
{
setlocale(LC_ALL, "chs");
size_t ret;
char mb[50];
wchar_t wc[] = L" I am a wide character ";
//wchar_t* wcStr = wc;
/* Convert wide character strings */
ret = wcstombs(mb, wc, BUFFER_SIZE);
printf(" Number of characters to convert = %u\n", ret);
printf(" Multibyte characters = %s\n\n", mb);
return(0);
}
Output
Number of characters to convert = 14
Multibyte characters = I am a wide character
- mbstowcs
size_t mbstowcs (wchar_t* dest, const char* src, size_t max);
Convert multibyte string to wide-character string
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#define BUFFER_SIZE 50
int main()
{
setlocale(LC_ALL, "chs");
char mbyte[50];
wchar_t * wbyte =NULL;
strcpy(mbyte, " I am a multibyte character ");
size_t mlen = strlen(mbyte);
int dSize = mbstowcs(wbyte, mbyte, 0) + 1;
wbyte = new wchar_t[dSize];
wmemset(wbyte, 0, dSize);
int len = mbstowcs(wbyte, mbyte, mlen);//mlen The length is longer than required , Will not fail .
return(0);
}
See :
1、wcstombs
2、mbstowcs()/wcstombs()
Four 、 The nature of wide and multibyte characters
see : What are the wide character codes and multi byte codes for Chinese characters
5、 ... and 、 other
1. Wide characters wchar_t And narrow characters char Difference and mutual transformation
2. be based on C++ Boost locale library , take utf8,utf16,utf32 Character sets are converted to and from each other
3.boost Library learning notes five Boost.Locale Character conversion gbk utf8 big5 string wstring etc.
4.windows API Realize Chinese string and GBK、Unicode、UTF-8 The three codes are converted to each other
5.boost::locale::conv:: Character encoding conversion
6. Yes C++ Local Classical analysis of
边栏推荐
- Knee joint
- Dynamic segment tree leetcode six hundred and ninety-nine
- svg中viewbox图解分析
- Seurat package addmodulescore is used for bulk RNA SEQ data
- [advanced pointer III] implement C language quick sorting function qsort & callback function
- Handling abnormal data
- Knee joint
- 利用nvm动态调整nodejs版本,解决因为node版本过高或过低导致项目无法运行和打包
- (P14) use of the override keyword
- [new planning]
猜你喜欢

Background position - exact units

(p19-p20) delegate constructor (proxy constructor) and inheritance constructor (using)

torch.logical_and()方法

Binlog in mysql:

《MATLAB 神经网络43个案例分析》:第8章 GRNN网络的预测----基于广义回归神经网络的货运量预测
![[GUI development] browsing function implementation model of image processing software](/img/37/2162a6047682b9cfc9b8b7c2488068.jpg)
[GUI development] browsing function implementation model of image processing software

JVM学习笔记:三 本地方法接口、执行引擎

MFS详解(四)——MFS管理服务器安装与配置

Background location case II

(p15-p16) optimization of the right angle bracket of the template and the default template parameters of the function template
随机推荐
JVM学习笔记:三 本地方法接口、执行引擎
Close asymmetric key
动态线段树leetcode.699
Adjust SVG width and height
Engineers learn music theory (I) try to understand music
【 pointeur avancé Ⅲ】 mise en œuvre de la fonction de tri rapide qsort& fonction de rappel en langage C
深拷贝与浅拷贝的区别
【字符集六】宽字符串和多字节字符互转
[advanced pointer I] character array & array pointer & pointer array
ISCSI详解(五)——ISCSI客户端配置实战
[sklearn] lightgbm
Detailed explanation of private, public and interface attributes in cmake
(p15-p16) optimization of the right angle bracket of the template and the default template parameters of the function template
Background location case II
Popular understanding of time domain sampling and frequency domain continuation
FDA审查人员称Moderna COVID疫苗对5岁以下儿童安全有效
UMI packaging and subcontracting, and compressing to gzip
Regular verification user name
第七章-更灵活定位内存地址
2022.6.11-----leetcode. nine hundred and twenty-six