如何将 UTF-16 与 ASCII 相互转换
我正在用 MIPS 汇编语言编写一个子例程,将 ASCII 转换为 UTF-16,反之亦然。但是,我找不到任何转换它的技巧。
I'm writing a subroutine in MIPS assembly language to convert ASCII into UTF-16 and vice versa. However, I could not find any trick how to convert it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
伪代码,假设您的字节是八位字节并且不需要零终止:
从 ASCII 到 UTF-16 的转换
MOVZX
。您还可以注意正确的字节顺序。从 UTF-16 到 ASCII 的无损转换
Pseudocode, assuming that your bytes are octets and that no zero termination is required:
Conversion from ASCII to UTF-16
MOVZX
. You may also pay attention to the correct endianness.Lossless conversion from UTF-16 to ASCII
术语 ASCII 并不是很具体。
ISO-646 是 Unicode UTF-16 的子集。因此,“7 位”ASCII 数字已经 Unicode(即您只需将它们放入 16 位值的底部),而对于另一个方向,您所要做的就是取低位如果这就是您的意思,则使用 Unicode 中的 8 位来获取 ASCII。
另一方面,如果您需要 ISO-8859-1 (Latin-1),则需要一个转换表。没有任何公式可以翻译成简单的汇编语言指令。
The term ASCII is not very specific.
ISO-646 is a subset of Unicode UTF-16. So '7-bit' ASCII numbers are already Unicode (i.e. you just drop them into the bottom of a 16 bit value), and, for the other direction, all you have to do is take the low 8 bits from Unicode to get the ASCII if this is what you mean.
If you need, on the other hand, ISO-8859-1 (Latin-1), you'll need a conversion table. There is no formula that can be translated into simple instructions in assembly language.
只要您只有 UCS2(仅 16 位代码点),您就可以通过执行
short <-> 直接转换为 ASCII。 char
-conversion 仅转换小于 128 的数字。As long as you only have UCS2 (only 16-bit codepoints) you can convert directly to ASCII by doing a
short <-> char
-conversion only converting numbers smaller than 128.