如何以编程方式更改完整文件的编码? [英] How do I change the encoding of of full file programatically ?

查看:78
本文介绍了如何以编程方式更改完整文件的编码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经导出了  windows注册表到.reg文件,它有编码UCS -2 little endian。我想将编码更改为ANSI。 Notepad ++只需几秒钟即可完成。如何在C ++中以编程方式执行此操作。 

I've exported  windows registry to .reg file which has encoding UCS -2 little endian. I wanted to change the encoding to ANSI. Notepad++ does this in fraction of seconds. How can I do this programmatically in C++. 

Nandkumar T。

Nandkumar T.

推荐答案

有一点需要指出的是,Windows最近使用的是UTF-16。它已经存在了大约18年,Windows 2000是使用UTF-16而不是UCS-2的Windows的第一个版本。这是一个重要的区别,因为UCS-2是一个固定宽度的编码
,不支持代理,因此无法访问0xFFFF以上的任何代码点。

One thing to point out is that Windows uses UTF-16 these days. It has for around 18 years now, with Windows 2000 being the first version of Windows to use UTF-16 as opposed to UCS-2. This is an important distinction because UCS-2 is a fixed width encoding that doesn't support the surrogates, so can't get access to any codepoints above 0xFFFF.

这非常简单。

首先打开文件并开始读取数据。如果你使用
CreateFile

ReadFile
然后你将不得不寻找字节顺序标记,它应该是第一个字符。您必须删除此字节顺序标记。如果你使用
fopen ,它接受ccs =作为模式的一部分string会自动执行此操作。

First open the file and start reading the data in. If you use CreateFile and ReadFile then you will have to look for the byte order mark, which should be the first character. You must remove this byte order mark. If you are using fopen, it accepts the ccs= as part of the mode string will do this automatically.

其次,将字符串转换为您希望它的代码页。这可以通过
来完成
WideCharToMultiByte

wctomb

Secondly, convert the string to the codepage that you want it to be. This can be done with WideCharToMultiByte or wctomb.

第三,写文件。请注意,如果您使用fcsn以ccs模式打开文件,则可能必须关闭该文件并在模式下重新打开该文件以编写ANSI版本。

Third, write the file. Be aware that if you are using fopen with the ccs mode to open the file, you may have to close the file and reopen it without the ccs in the mode to write the ANSI version.


这篇关于如何以编程方式更改完整文件的编码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆