当前位置:网站首页>[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
边栏推荐
- 2022.6.9-----leetcode. four hundred and ninety-seven
- How to understand the production scheduling of APS system?
- Engineers learn music theory (III) interval mode and chord
- Only use MES as a tool? Looks like you missed the most important thing
- Get last month, current time and next month
- 2022 safety officer-c certificate special operation certificate examination question bank and simulation examination
- Adjust SVG width and height
- Encapsulate the amount input box component.
- 2022.6.11-----leetcode.926
- What scheduling rules does the APS software have? What is the exception handling scheme?
猜你喜欢

Adjust SVG width and height

Production scheduling status of manufacturing enterprises and solutions of APS system

Where does the driving force of MES system come from? What problems should be paid attention to in model selection?

【字符集九】gbk拷贝到Unicode会乱码?

Analysis of 43 cases of MATLAB neural network: Chapter 7 regression of RBF Network -- Realization of nonlinear function regression

根据有效期显示距离当前还剩多少天有效期

FDA reviewers say Moderna covid vaccine is safe and effective for children under 5 years of age

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

Set up redis sentinel cluster (instance):

Loading circling effect during loading
随机推荐
Installation of Shengxin R package
Summary of common character sets
Only use MES as a tool? Looks like you missed the most important thing
Background position position NOUN
第五章-[bx]和Loop指令
Hypergeometric cumulative distribution test overlap
How to understand the production scheduling of APS system?
[untitled] task3 multiple recall
Problems that cannot be resolved by tar command
【 pointeur avancé Ⅲ】 mise en œuvre de la fonction de tri rapide qsort& fonction de rappel en langage C
进制GB和GiB的区别
Notes used by mqtt (combined with source code)
(p19-p20) delegate constructor (proxy constructor) and inheritance constructor (using)
【动态内存管理】malloc&calloc和realloc和笔试题和柔性数组
Judge whether the object is empty
2022.6.9-----leetcode.497
【字符集七】汉字的宽字符码和多字节码分别是多少
The newline character with in the string is converted to an array
When converting tensor to ndarray in tensorflow, the run or Eval function is constantly called in the loop, and the code runs more and more slowly!
第七章-更灵活定位内存地址