UTF-8在Windows [英] UTF-8 in Windows

查看:300
本文介绍了UTF-8在Windows的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何在C Windows程序设置code页为UTF-8?

How do I set the code page to UTF-8 in a C Windows program?

我有了用途的fopen打开文件的第三方库。我可以用wcstombs我的Uni code文件名转换为当前code页面,但是如果用户拥有与code页面那么这个符之外的字符的文件名。

I have a third party library that has uses fopen to open files. I can use wcstombs to convert my Unicode filenames to the current code page, however if the user has a filename with a character outside the code page then this breaks.

理想我只想叫_setmbcp(65001)于code页面设置为UTF-8,但是,对于_setmbcp MSDN文档指出,UTF-8不支持。

Ideally I would just call _setmbcp(65001) to set the code page to UTF-8, however the MSDN documentation for _setmbcp states that UTF-8 is not supported.

我怎样才能解决这个问题?

How can I get around this?

推荐答案

不幸的是,没有办法让统一code在Windows当前codePAGE。在 CP_UTF7 CP_UTF8 常量是假codepages,只有在的MultiByteToWideChar 并的WideCharToMultiByte 转换功能,如奔提及。

Unfortunately, there is no way to make Unicode the current codepage in Windows. The CP_UTF7 and CP_UTF8 constants are pseudo-codepages, used only in MultiByteToWideChar and WideCharToMultiByte conversion functions, like Ben mentioned.

您的问题是类似于fstream的C ++类的。该fstream的构造函数只接受的char * 的名字,使得无法打开一个真正的统一code名称的文件。由VC提供的唯一解决方案是一个黑客:单独打开该文件,然后设置的句柄流对象。恐怕这不是一个选择,当然,由于第三方库很可能不接受把手。

Your problem is similar to that of the fstream C++ classes. The fstream constructors accept only char* names, making impossible to open a file with a true Unicode name. The only solution offered by VC was a hack: open the file separately and then set the handle to the stream object. I'm afraid this isn't an option for you, of course, since the third party library probably doesn't accept handles.

我能想到的唯一解决方案是创建一个非统一code的名字,这是硬链接到原始一个临时文件,并使用它作为一个参数。

The only solution I can think of is to create a temporary file with a non-Unicode name, which is hard-linked to the original, and use that as a parameter.

这篇关于UTF-8在Windows的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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