使用 ICU 库将 UTF-8 转换为 ASCII
我有一个 std::string ,其中包含 UTF-8 字符。
我想将字符串转换为最接近的 ASCII 字符。
例如:
Łódź => 罗兹
Assunção =>; 阿松桑
城堡 => Schloss
不幸的是 ICU 库真的很不直观,我还没有找到关于它的使用的好的文档,所以我花了太多时间来学习使用它。 我没有时间。
有人可以举一个小例子来说明如何做到这一点吗?
谢谢。
I have a std::string with UTF-8 characters in it.
I want to convert the string to its closest equivalent with ASCII characters.
For example:
Łódź => Lodz
Assunção => Assuncao
Schloß => Schloss
Unfortunatly ICU library is realy unintuitive and I haven't found good documentation on its usage, so it would take me too much time to learn to use it. Time I dont have.
Could someone give a little example about how can this be done??
thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
尝试这个,
ucnv_convert(“US-ASCII”,“UTF-8”,目标,目标大小,源,源大小,pError)
Try this,
ucnv_convert("US-ASCII", "UTF-8", targer, targetsize, source, sourcesize, pError)
我不了解 ICU,但 ICONV 可以做到这一点,而且非常容易学习。 只需大约 3-4 次调用,您在您的情况下需要的是使用
iconvctl()
来使用ICONV_SET_TRANSLITERATE
标志。I don't know about ICU but ICONV does this and its quite easy to learn. it's only about 3-4 calls and what you need in your case is to use the
ICONV_SET_TRANSLITERATE
flag usingiconvctl()
.我编写了一个分解然后进行一些替换的回调。 它可能可以作为音译来实现。 代码在这里 decompcb.c 和标头就在附近。 在 Unicode 到 ASCII 转换器上安装如下:
然后使用 gConverter 将 unicode 转换为 ASCII
I wrote a callback that decomposes and then does some substitution. It could probably be implemented as a transliteration. code is here decompcb.c and header is nearby. Install it as follows on a Unicode-to-ASCII converter:
then use gConverter to convert from unicode to ASCII
这不是我擅长的领域,但如果您没有方便的库可以轻松地为您完成此操作,那么您最好创建一个包含 UTF-8 -> 的查找表/映射; ASCII 值。 IE。 键是 UTF-8 字符,值是字符的 ASCII 序列。
This isn't an area I'm an expert in, but if you don't have a library handy that does it for you easily then you might be better of just creating a lookup table/map which contains the UTF-8 -> ASCII values. ie. The key is the UTF-8 char, the value is the ASCII sequence of chars.
ß->ss 分解告诉我您想要兼容性分解。 在 ICU 中,您需要类 Normalizer。 之后,你会得到类似 L'odz' 的东西。
您可以简单地从该字符串中删除非 ASCII 字符。 不需要ICU,简单的STL就可以了。
The ß->ss decomposition tells me you want the compatibility decomposition. In ICU, you need class Normalizer for that. Afterwards, you will end up with something like L'odz'.
From this string, you can simply remove the non-ASCII characters. No need for ICU, plain STL will do.