用 Sublime Text 2 保存中文后乱码问题的解决方法(可能解决)
在群里面听到有人说起有一款新的编辑器 Sublime Text 2 很不错, 于是我下载下来试用了一下, 发现不支持
中文, 听说要安装什么插件, 不管了, 没打算用, 但... 我打开了我的一个源代码C文件, 突然发现了一点小错误, 就用
ST2修改后Ctrl+S保存了, 当时没在意什么, 到了几天后的今天, 一看, 突然代码中的中文全部变成了乱码, 用强大的Notepad++
也没能识别出文件的编码, 结果, 只好用 WinHex 查看 16进制 内容, 咋一看, 着实吓了我一跳, 中文居然被编码成1个汉字
4个字节, 而且还很有规律, ...
规律是每个汉字被加上了两个字节的C2/C3, 不懂是为什么!
比如原来一个中文的两个字节:11 22
结果可能就成为:C2 11 C2 22
擦, 无语, 吓得我汗下, 那么多的代码, 中文全没了....
所有的规律(数据为16进制):
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
乱码后:
源代码:
#include <stdio.h> #include <string.h> #include <stdlib.h> //女孩不哭 QQ:191035066 2013-05-29 20:35 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:命令行参数不正确,缺少文件!\n"); return 1; } fp = fopen(argv[1],"rb"); if(fp == NULL){ fprintf(stderr,"st2_dec:不能打开文件:%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:空白文件!\n"); fclose(fp); return 2; } buffer = (unsigned char*)malloc(file_len+1); if(buffer == NULL){ fprintf(stderr,"st2_dec:分配缓冲区失败!\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:创建输出文件时失败!\n"); free(buffer); return 4; } fwrite(buffer,1,file_len,fp); fclose(fp); free(buffer); printf("st2_dec:成功, 输出到文件:st2_dec.txt\n"); return 0; }
VC6项目及EXE下载:
https://files.cnblogs.com/nbsofer/st2_dec.7z
http://alioss.twofei.com/windows/st2_dec.7z
女孩不哭 @ 2013-05-29 20:54:57 @ http://www.cnblogs.com/nbsofer