在中英文混合的文本输入输出中,怎么统一编码格式
在中英文混合的文本输入输出中,如何统一编码格式?
在VC++中,对中英文混合文本处理,输入中英文混合文本,默认是:UTF-8编码,
然后处理之后输出中英文混合文本,由于在输出时设置了相应的输出提醒:ANSI编码,
导致编码不统一,若打开方式改为utf-8:
问一下 如何修改?
------解决方案--------------------
对电脑而言没有乱码,只有二进制字节;对人脑才有乱码。啊 GBK:0xB0 0xA1,Unicode-16 LE:0x4A 0x55,Unicode-16 BE:0x55 0x4A,UTF-8:0xE5 0x95 0x8A
推荐使用WinHex软件查看硬盘或文件或内存中的原始字节内容。
wstring
In Visual C++ 2005, fopen supports Unicode file streams. A flag specifying the desired encoding may be passed to fopen when opening a new file or overwriting an existing file, like this:
fopen("newfile.txt", "rw, ccs=<encoding>");
Allowed values of the encoding include UNICODE, UTF-8, and UTF16-LE. If the file is already in existence and is opened for reading or appending, the Byte Order Mark (BOM) is used to determine the correct encoding. It is not necessary to specify the encoding with a flag. In fact, the flag will be ignored if it conflicts with the type of the file as indicated by the BOM. The flag is only used when no BOM is present or if the file is a new file. The following table summarizes the modes used in for various flags given to fopen and Byte Order Marks used in the file.
Encodings Used Based on Flag and BOM
Flag No BOM (or new file) BOM: UTF-8 BOM: UTF-16
UNICODE
ANSI
UTF-8
UTF-16LE
UTF-8
UTF-8
UTF-8
UTF-16LE
UTF-16LE
UTF-16LE
UTF-8
UTF-16LE
------解决方案--------------------
既然你要保存到一个ANSI编码的文件,你就必须要进行编码转换啊。最好程序内用Unicode。所以你需要mbstowcs或者MultiByteToWideChar把UTF-8转换为UTF-16。处理完后在转wcstombs、WideCharToMultiByte为ANSI
你的编码转换的代码在哪里呢?
另外你的问题有点儿含糊,建议你重新整理一下,详细说明问题和场景。
比如什么是输出提醒?程序内以什么方式处理的呢?
------解决方案--------------------
同意7楼,读写时要用MultiByteToWideChar和WideCharToMultiByte API
或者c库函数mbstowcs和wcstombs进行转换。
------解决方案--------------------
应该可以
在VC++中,对中英文混合文本处理,输入中英文混合文本,默认是:UTF-8编码,
鄂 宝 汽车 销售 服务 有限公司 Add / 地址 : 武汉 盘 龙城 经济 开发区 盘 龙 汽车城 Panlong Auto - City Panlong City Economic Zone Wuhan Tel / 电
MERRY CHRISTMAS AND HAPPY NEW YEAR Dear Valued Customers
然后处理之后输出中英文混合文本,由于在输出时设置了相应的输出提醒:ANSI编码,
重新分类的结果如下:?楂樻 Wuhan EBAO Auto Sales & Services Co. Ltd. / 姝︽眽 閯?瀹?姹借溅 閿€鍞?鏈嶅姟 鏈夐檺鍏徃 Add / 鍦板潃 锛?姝︽眽 鐩?榫欏煄 缁忔祹 寮€鍙戝尯 鐩?榫?姹借溅鍩?Panlong Auto - City Panlong City Economic Zone Wuhan Tel / 鐢佃瘽 锛?
导致编码不统一,若打开方式改为utf-8:
۳טтؖ`քޡڻɧЂ: 武汉 盘 龙城 经济 开发区 盘 龙 汽车城 Panlong Auto - City Panlong City Economic Zone Wuhan Tel / 电话 ۳ҩمÊ:
问一下 如何修改?
------解决方案--------------------
对电脑而言没有乱码,只有二进制字节;对人脑才有乱码。啊 GBK:0xB0 0xA1,Unicode-16 LE:0x4A 0x55,Unicode-16 BE:0x55 0x4A,UTF-8:0xE5 0x95 0x8A
推荐使用WinHex软件查看硬盘或文件或内存中的原始字节内容。
wstring
In Visual C++ 2005, fopen supports Unicode file streams. A flag specifying the desired encoding may be passed to fopen when opening a new file or overwriting an existing file, like this:
fopen("newfile.txt", "rw, ccs=<encoding>");
Allowed values of the encoding include UNICODE, UTF-8, and UTF16-LE. If the file is already in existence and is opened for reading or appending, the Byte Order Mark (BOM) is used to determine the correct encoding. It is not necessary to specify the encoding with a flag. In fact, the flag will be ignored if it conflicts with the type of the file as indicated by the BOM. The flag is only used when no BOM is present or if the file is a new file. The following table summarizes the modes used in for various flags given to fopen and Byte Order Marks used in the file.
Encodings Used Based on Flag and BOM
Flag No BOM (or new file) BOM: UTF-8 BOM: UTF-16
UNICODE
ANSI
UTF-8
UTF-16LE
UTF-8
UTF-8
UTF-8
UTF-16LE
UTF-16LE
UTF-16LE
UTF-8
UTF-16LE
------解决方案--------------------
既然你要保存到一个ANSI编码的文件,你就必须要进行编码转换啊。最好程序内用Unicode。所以你需要mbstowcs或者MultiByteToWideChar把UTF-8转换为UTF-16。处理完后在转wcstombs、WideCharToMultiByte为ANSI
你的编码转换的代码在哪里呢?
另外你的问题有点儿含糊,建议你重新整理一下,详细说明问题和场景。
比如什么是输出提醒?程序内以什么方式处理的呢?
------解决方案--------------------
同意7楼,读写时要用MultiByteToWideChar和WideCharToMultiByte API
或者c库函数mbstowcs和wcstombs进行转换。
------解决方案--------------------
应该可以