将C字符串从本地编码转换为UTF8 [英] Converting C-Strings from Local Encoding to UTF8
问题描述
我在写一个小应用程序,其中我从控制台读取一些文本,然后存储在经典的char *字符串。
因为它发生,我需要将它传递到一个lib它只采用UTF-8编码字符串。由于Windows控制台使用本地编码,我需要从本地编码转换为UTF-8。
如果我没有错误,我可以使用MultiByteToWideChar(..)编码为UTF-16和然后使用WideCharToMultiByte(..)转换为UTF-8。
I'm writing a small App in which i read some text from to console, which is then stored in a classic char* string.
As it happens i need to pass it to an lib which only takes UTF-8 encoded Strings. Since the Windows console uses the local Encoding, i need to convert from local encoding to UTF-8.
If i'm not mistaken i could use MultiByteToWideChar(..) to encode to UTF-16 and then use WideCharToMultiByte(..) to Convert to UTF-8.
然而,我想知道是否有一种方法直接从本地编码转换为UTF-8而不使用任何外部Libs,因为转换为wchar的想法只是
However i wonder if there is a way to convert directly from local Encoding to UTF-8 without the use of any external Libs, since the idea of converting to wchar just to be able to convert back to char (utf-8 encoded but still) seems kinda weird to me.
推荐答案
从UTF-8转换为char(utf-8编码但仍然) 16到UTF-8是一个纯粹的机械过程,但从本地编码转换为UTF-16或UTF-8涉及一些大的专用查找表。 c运行时只是转向,并调用WideCharToMultiByte和MultiByteToWideChar为非平凡的情况。
Converting from UTF-16 to UTF-8 is purely a mechanical process, but converting from local encoding to UTF-16 or UTF-8 involves some large specialized lookup tables. The c-runtime just turns around and calls WideCharToMultiByte and MultiByteToWideChar for non-trivial cases.
至于必须使用UTF-16作为中间阶段,知道,没有任何方法 - 对不起。
As for having to use UTF-16 as an intermediate stage, as far as I know, there isn't any way around that - sorry.
由于您已经链接到外部库以获取文件输入,您可以链接到同一个库来获取WideCharToMultiByte和MultiByteToWideChar。
Since you are already linking to an external library to get file input, you might as well link to the same library to get WideCharToMultiByte and MultiByteToWideChar.
使用c-runtime将使您的代码可以重新编译到其他操作系统(理论上),但它也增加了一层开销,你和库之间在这种情况下做所有的真正的工作 - kernel32.dll。
Using the c-runtime will make your code re-compilable to other operating systems (in theory), but it also adds a layer of overhead between you and the library that does all of the real work in this case - kernel32.dll.
这篇关于将C字符串从本地编码转换为UTF8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!