Conversion of Chinese to Unicode code using C language __c language

Source: Internet
Author: User

Because I like to use the notepad++ editor, the editor's advantage is small and flexible, but there are a few places to do, but I can take appropriate measures to replace, let us look at the notepad++ where the deficiencies, and then take what measures.


One: notepad++ can not open 16 into the file, UE may be achieved;

Common solution: Our commonly used beyond Compare 4 (text comparison) can easily replace the notepad++ cannot see the problem of the 16 system.

Two notepad++ can not achieve the alphabet, Chinese characters converted into ASCII code, Uniconde code. Using UE can be easily replaced.

Common solution: As the following code written, under the Linux command to compile the build executable file, and then you can run, you can achieve the corresponding function conversion, I write this gadget is to facilitate my own use.

Some friends do not understand, since the UE can be so good to achieve the function, why do not you choose to use the UE, so I am here I have to say, this look a person likes, I like notepad++, compact, intuitive interface, free.


Unicode Chinese encoding table: please click here. Character encoding notes: Ascii,unicode and UTF-8: please click here.

Convert Chinese to Uniconde code: MAIN.C

#include <stdio.h> #include <string.h>
#include <math.h>
#include <wchar.h>

int main ()
{
wchar_t *CHINESE_STR = L "Network Man vs Gray Pigeon";
unsigned int *p = (wchar_t *) chinese_str;
int i;
printf ("UNIOCDE: \ n");
for (i = 0; i < wcslen (CHINESE_STR); i++)
{
printf ("0x%x", P[i]);

}

printf ("\ n");
return 0;

}


Compiling: Gcc-finput-charset=gbk-fexec-charset=utf-8-o main main.c

The above compiler command you do it, if you do not take the above parameters compiled will be an error, as follows:

Main.c:8:24:error:converting to execution character set:invalid or incomplete multibyte or wide

This is the problem of coding, if you write code in the WinDOS format that is saved in GBK,GCC encoding format is UTF-8 by default.

The source file is written in a different encoding, which results in a difference in execution.

Man gcc,/charset/* View the format of GCC encoding and running code
-finput-charset=charset represents the encoding of the source file, which is resolved by default UTF-8
-fexec-charset=charset represents the encoding of the words in the executable program, and the default is UTF-8

./main

UNIOCDE:
0x7f51 0x7edc 0x4eba 0x56 0x53 0x7070 0x9e3d 0x5b50

。。。。 The above code can be tested. Open a notepad on your PC and write the program, save it in a different format. Can try it on your own. Self-test can deepen your understanding.


Enables conversion between characters and ASCII code (not very concise)

/* Gets Example * *
#include <stdio.h>
#include <string.h>
#include <stdlib.h>


void Print_usage (char *file)
{
/* Gets Example * *
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

Void Print_usage (char *file)
{
  printf ("%s c/a\n", file);
}
int main (int argc,char **argv)
{
  char str;
  int  cnt;


  if (argc!=2)
{
Print_usage (argv[0]);
return-1;
}

if (strcmp (argv[1], "C") ==0)
{
   printf ("Insert your Ascail:");
  &NBSP;SCANF ("%x", &cnt);
   printf ("Your Ascail is:%d  0x%x '%c ' \ n", cnt,cnt,cnt);
}
if (strcmp (argv[1], "a") ==0)
{
  printf (Insert your char:);
&NBSP;SCANF ("%c", &str);
 printf ("Your Ascail is:%d  0x%x   ' \ n", str,str,str);
}
 return 0;
}
Compiling: &NBSP;GCC main.c-o mian      

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.