2014-01-13 29 views
2

我有一些错误解码的文本片段。它被解码,如cp866,但实际上它应该是utf-8"нажал кабан на баклажан""╨╜╨░╨╢╨░╨╗ ╨║╨░╨▒╨░╨╜ ╨╜╨░ ╨▒╨░╨║╨╗╨░╨╢╨░╨╜")。我想解决这个问题,我已经写在的Python代码解决了任务:字节解码在D

broken = "╨╜╨░╨╢╨░╨╗ ╨║╨░╨▒╨░╨╜ ╨╜╨░ ╨▒╨░╨║╨╗╨░╨╢╨░╨╜" 
fixed = bytes(broken, 'cp866').decode('utf-8') 
print(fixed) # it will print 'нажал кабан на баклажан' 

然而,一开始我是想在d来解决这个问题,但没有成功找到答案。那么,如何在D中解决这个问题呢?

+0

[std.encoding(http://dlang.org/phobos/std_encoding.html)会有帮助,但它并不像CP866支持 –

回答

4

目前,D没有广泛的原生设施来在编码之间转换文本。

这里有一些选择:

  • 随着棘轮怪胎提到,d确实有std.encoding,但不包括在目前许多编码。
  • 在Windows上,您可以使用std.windows.charset.fromMBSztoMBSz,其中包含MultiByteToWideCharWideCharToMultiByte
  • 您可以简单地将您感兴趣的编码嵌入到程序中(example)。
  • 在POSIX上,您可以调用iconv程序(example),或使用libiconv库(D1 binding)。
+0

建议有关** std.windows.charset * *非常有用。 Windows的溶剂类似于'fixed = fromMBSz(cast(immutable)toMBSz(broken,866),65001)' –