我用“记事本”打开某个数据文件data.txt,其中中文"本地主机"显示正常。但我用ultraedit打开时显示为乱码。切换到HEX模式,显示为 -- E6 9C AC E5。但我用“记事本”实际输入"本地主机",生成新文件。然后用ultraedit打开,切换到HEX模式,显示为 -- B1 BE B5 D8。我想问的是data.txt"本地主机"编码-- E6 9C AC E5是什么编码?为什么用ultraedit打开是乱码,而用“记事本”打开显示正常?
调试欢乐多
你没有数据文件data.txt .:(数据文件data.txt好象并不是纯文本格式, 但不知道如何转换?
#include <stdlib.h>int main()
{
FILE *in,*out;
char infile[100],outfile[100];
unsigned char ch,cha,chb;
int i,j,k,size; printf("Enter the infile name: \n");
scanf("%s",infile);
printf("Enter the outfile name: \n");
scanf("%s",outfile); if ((in=fopen(infile,"rb"))==NULL)
{
printf("Can not open infile!\n");
exit(0);
} if((out=fopen(outfile,"w"))==NULL)
{
printf("Can not open outfile!\n");
exit(0);
}
size=0;
while(!feof(in))
{
fgetc(in);
size++;
} printf("%d\n",size);
j=1;
k=0;
rewind(in);
while (k<size-1)
{
k++;
ch=fgetc(in);
cha=(unsigned char)0x80;
for (i=0;i<8;i++)
{
chb=ch&cha;
chb=chb>>7;
fprintf(out, "%d", chb );
ch=ch<<1;
}
if (i==8)
fputc(' ',out);
if (j%4==0)
fputc('\n',out);
j++;
} fclose(in);
fclose(out);
return 0;
}/*二进制文件:010101010101010101010001
*文本文件:0101 0101 0101 0101
0101 0001
*/
如何转化成 B1 BE B5 D8?
B1 BE B5 D8是GBK的繁体汉字
可用
LCMapString(0x0804,LCMAP_TRADITIONAL_CHINESE, pszGbs, -1, pszGbt, len);
转
B1 BE B5 D8是GBK的简体汉字
char str [ 256 ] = {0xE6,0x9c, 0xAc, 0xE5 ,0} ; //一段UTF-8编码
WCHAR* strA;
int i= MultiByteToWideChar ( CP_UTF8 , 0 ,(char*) str ,-1 ,NULL,0);
strA = new WCHAR[i];
MultiByteToWideChar ( CP_UTF8 , 0 ,( char * ) str, -1, strA , i );
i= WideCharToMultiByte(CP_ACP,0,strA,-1,NULL,0,NULL,NULL);
char *strB=new char[i];
WideCharToMultiByte (CP_ACP,0,strA,-1,strB,i,NULL,NULL);
//strB即为所求