Code conversion with C language

zhaozj2021-02-16  50

About the internal code of the internal code conversion, there are many articles on the Internet, and there is a source code, there are also many in 9cbs. As far as I know, there are two ways to be more common. Take GB and BIG5 as an example, one can build a one-one corresponding table. If you want to use, you can use Windows powerful multi-language processing functions, such as WideChartomultibyte and MultibytetowideChar these two functions. They use unicode to transfer, implement the conversion between the two code pages.

These have the prior person to describe before, I will not say much. I have mentioned in the topic that it is necessary to use the C language to convert, the first method is naturally achievable, but I don't want my code to be lengthy, this big table can still be good, Save trouble. As for the second method, you must deal with the operating system, more importantly, I don't like the style of the Windows API, so I don't want to use it.

However, the final method is still similar to the second method, but I don't have to use the Windows API, I use the standard C function.

In fact, long ago, C will have a good support for Unicode and localization, two functions of WCSTombs and MBstowcs are similar to the two Windows APIs I mentioned earlier, and the principle of implementing internal conversion is similar. The only thing to note is that the code page in the Windows API is set directly in the function, while the C language is set throughout the run.

Setting the function when running is setlocale, using it needs to include the header file , using WCSTombs and MBSTOWCS, you want to include the header file . Take Chinese as an Example 936 code page is Simplified, 950 is traditional, of course, can also be converted to other Chinese character internal code pages, such as UTF8. Here is an example.

#include #include #include

INT main (void) {char * Temp; char sim [32] = "dog"; char TRA [32]; wchar_t uni [32]; temp = setLocale (lc_all, ". 936); PUTS (TEMP); Printf ("% S / T% 2x% 2x / R / N", SIM, (Uns [0]), (SIM [1])); if (-1 == Mbstowcs (Uni , SIM, 32)) {PERROR ("MBSTOWCS");} Temp = setLocale (lc_all, ". 950); if (-1 == WCSTOMBS (TRA, UNI, 32)) {PERROR (" WCSTombs "); } Printf ("% S / T% 2x% 2x / R / N", TRA, (TRA [0]), (TRA [1])); System ("Pause"); Return 0;}

转载请注明原文地址:https://www.9cbs.com/read-15446.html

New Post(0)