简体   繁体   English

如何在 Linux 上将 Windows-1251(ISO-88-59-5) 字符串转换为 UTF-8 字符串?

[英]How to convert Windows-1251(ISO-88-59-5) string to UTF-8 string on Linux?

I have a common string, which is encoded like ISO-88-59-5 and I want to transform this string to UTF-8 format, by the way, I have the code example on C# which is working well.我有一个通用字符串,其编码为 ISO-88-59-5,我想将此字符串转换为 UTF-8 格式,顺便说一下,我有 C# 上的代码示例,它运行良好。 I need to do the same on C++我需要在 C++ 上做同样的事情

result = mainString.Substring(nameStart + 3, symbols);
Encoding enc = Encoding.GetEncoding("ISO-8859-5");
byte[] bytes = enc.GetBytes(result);
                
result = Encoding.UTF8.GetString(bytes);

result is a string with text结果是一个带有文本的字符串

The procedure to do this on Linux is as follows:在 Linux 上执行此操作的过程如下:

  1. Use iconv_open() as described in its manual page to create a handle for a conversion from windows-1251 to UTF-8.使用其手册页中描述的iconv_open()来创建从 windows-1251 到 UTF-8 的转换的句柄。 I just double-checked and "windows-1251" is supported by the iconv library.我只是仔细检查了一下,iconv 库支持“windows-1251”。

  2. Use iconv() as described in its manual page .按照其手册页中的说明使用iconv()

  3. Use iconv_close() as described in its manual page .按照其手册页中的说明使用iconv_close()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM