How to solve the garbled problem after saving Chinese characters with sublime text 2 (possible solution)

Source: Internet
Author: User

Someone in the group said that there was a new editor sublime text 2, which was very good. So I downloaded it and tried it. I found it not supported.
(Chinese) I heard about the plug-ins to be installed. No matter what plug-ins, I don't plan to use them, but... I opened one of mySource codeC file, suddenly found a small error, Use
After st2 was modified, CTRL + S saved it. At that time, I didn't care much about it. Today, a few days later, I saw it suddenlyCodeAll the Chinese characters in are garbled, with the powerful notepad ++
I failed to identify the file encoding. As a result, I had to use winhex to view the hex content. At first glance, I was shocked and the Chinese was actually encoded into one Chinese character.
4 bytes, and it is still quite regular ,...

The rule is that each Chinese character is added with two bytes of C2/C3. Why!
For example, the original Chinese two bytes: 11 22
The result may be: C2 11 C2 22.

Wipe, speechless, scared me to sweat, so much code, Chinese is all gone ....

All rules (hexadecimal data ):
C3 X1 C3 X2 => X1 + 40, X2 + 40
C3 X1 C2 X2 => X1 + 40, X2
C2 X1 C3 X2 => x1, x2 + 40
C2 X1 C2 X2 => x1, x2

After garbled:

Source code:

# Include <stdio. h> # Include < String . H># Include <Stdlib. h> //  Girls don't cry QQ: 191035066  Int Replace (unsigned Char * Orig, unsigned Char * Result, Int * Result_len) {unsigned  Char * PS = Result;  While (* Orig ){  If (* Orig <0x80  ){ * PS ++ = * Orig; orig ++ ;}  Else   If (Orig [ 0 ] = 0xc2 & Orig [ 2 ] = 0xc2  ){  //  C2 X1 C2 X2 => x1, x2 * PS ++ = orig [ 1 ]; * PS ++ = orig [ 3  ]; Orig + = 4  ;}  Else   If (Orig [ 0 ] = 0xc2 & Orig [ 2 ] = 0xc3  ){  //  C2 X1 C3 X2 => x1, x2 + 40 * PS ++ = orig [1  ]; * PS ++ = orig [ 3 ] + 0x40  ; Orig + = 4  ;}  Else   If (Orig [ 0 ] = 0xc3 & Orig [ 2 ] = 0xc2  ){  // C3 X1 C2 X2 => X1 + 40, X2 * PS ++ = orig [ 1 ] + 0x40  ; * PS ++ = orig [ 3  ]; Orig + = 4  ;}  Else   If (Orig [ 0 ] = 0xc3 & Orig [ 2 ] = 0xc3 ){  //  C3 X1 C3 X2 => X1 + 40, X2 + 40 * PS ++ = orig [ 1 ] + 0x40  ; * PS ++ = orig [ 3 ] + 0x40  ; Orig + = 4  ;}} * Result_len = (unsigned Int ) PS-(unsigned Int ) Result;  Return   1  ;}  Int Main ( Int Argc, Char ** Argv) {File * Fp = NULL; size_t file_len = 0  ; Unsigned  Char * Buffer = NULL;  If (Argc! =2  ) {Printf (  "  St2_dec: the command line parameter is incorrect. The file is missing! \ N  "  );  Return   1  ;} Fp = Fopen (argv [ 1 ], "  RB  "  );  If (FP = Null) {fprintf (stderr, "  St2_dec: file cannot be opened: % s \ n  " , Argv [ 1  ]);  Return   1  ;} Fseek (FP,  0  , Seek_end); file_len = Ftell (FP); fseek (FP,  0  , Seek_set );  If (File_len = 0 ) {Fprintf (stderr,  "  St2_dec: blank file! \ N  "  ); Fclose (FP );  Return   2  ;} Buffer = (Unsigned Char *) Malloc (file_len + 1  );  If (Buffer = Null) {fprintf (stderr,  " St2_dec: failed to allocate the buffer! \ N  "  ); Fclose (FP );  Return   3  ;} Memset (buffer,  0 , File_len + 1  ); Fread (buffer,  1  , File_len, FP); fclose (FP); Replace (buffer, buffer, & File_len); FP = Fopen ( "  St2_dec.txt " , "  WB  "  );  If (FP = Null) {fprintf (stderr,  "  St2_dec: An error occurred while creating the output file! \ N  "  ); Free (buffer );  Return   4  ;} Fwrite (buffer,  1 , File_len, FP); fclose (FP); free (buffer); printf (  "  St2_dec: Success, output to file: st2_dec.txt \ n  "  );  Return   0  ;} 

 
Vc6 project and exe download: http://files.cnblogs.com/nbsofer/st2_dec.7z

Girl don't cry @ 20:54:57 @ http://www.cnblogs.com/nbsofer

Related Article

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.