如何将 128 到 255 的字节映射到等效的 UTF16-LE 代理项对
我正在尝试实现这一目标:
我在 java web 服务中有一个 PDF byte[],我必须将其作为 base64 字符串发送到 .NET 客户端,该客户端会执行此操作以重建文件。
Encoding.Convert(Encoding.Unicode, Encoding.Default, Convert.FromBase64String(inputJava))
我无法更改客户端代码,现在 java web 服务正在调用另一个 .NET web 服务,该服务将 byte[] 转换为 base64 字符串:
System.Text.Encoding.Convert(System.Text.Encoding.GetEncoding(1252), System.Text.Encoding.Unicode, b);
除了我可以通过各种方式制作的 base64 之外(例如使用 org.apache .commons.codec.binary.Base64),我必须将原始 byte[] 转换为 UTF-16LE byte[]...
我尝试了这个:
byte[] output = new byte[b.length * 2];
for(int i=0; i < b.length; i++)
{
int val = b[i];
if(val < 0) val += 256;
output[2*i + 0] = (byte) (val);
output[2*i + 1] = 0;
}
这对于低于 128 的值(例如 1 => 0100, 2 => 0200, ... , 127 => 7F00) 但对于高于 (128 -> 255) 的值,我不知道如何获得等效的 2 字节值;我知道对于字节 156 (9C),对应的值为 8301 (0x5301),对于字节 224 (E0),对应的值为 12501 (0x7D01),但我没有找到一种算法来获取所有其他值。
字节值和相应的 UTF-16LE 代理对之间是否存在映射表或将值从 128 映射到 255 的算法?
提前致谢!
I'm trying to achieve this:
I have a PDF byte[] in java web service that I must send as a base64 string to a .NET client that does this to reconstruct the file.
Encoding.Convert(Encoding.Unicode, Encoding.Default, Convert.FromBase64String(inputJava))
I cannot change the client code and right now the java web service is calling another .NET web service that does this to turn the byte[] into a base64 string:
System.Text.Encoding.Convert(System.Text.Encoding.GetEncoding(1252), System.Text.Encoding.Unicode, b);
Beside the base64 that I can make in various ways (e.g. with org.apache.commons.codec.binary.Base64), I have to turn the original byte[] into a UTF-16LE byte[]...
I tried this:
byte[] output = new byte[b.length * 2];
for(int i=0; i < b.length; i++)
{
int val = b[i];
if(val < 0) val += 256;
output[2*i + 0] = (byte) (val);
output[2*i + 1] = 0;
}
This works fine for values below 128 (e.g. for 1 => 0100, 2 => 0200, ... , 127 => 7F00) but for values above (128 -> 255) I don't know how to get the equivalent 2bytes values; I know that for byte 156 (9C) the corresponding value is 8301 (0x5301) and for byte 224 (E0) the corresponding value is 12501 (0x7D01) but I didn't manage to find an algorithm to get all the other values.
Is there a mapping table between byte value and the corresponding UTF-16LE surrogate pair or an algorithm to map values from 128 to 255?
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你不需要代理对;它们是用于处理基本多语言平面 (BMP) 之外的字符的构造,并且所有 windows-1252 字符均采用 BMP。
官方的 windows-1252(别名 cp1252)到 Unicode 映射表是
http://unicode.org/Public/MAPPINGS/VENDORS/MICSFT/WINDOWS /CP1252.TXT
它是一个易于处理格式的纯文本文件,因此如果您找不到用于转换的现有工具,那么根据该文件编写映射应该相当简单。
该文件在官方 IANA 注册表中被间接引用:
http://www.iana.org/assignments/character-sets
You don’t need surrogate pairs; they are a construct for dealing with characters outside Basic Multilingual Plane (BMP), and all windows-1252 characters are in BMP.
The official windows-1252 (alias cp1252) to Unicode mapping table is
http://unicode.org/Public/MAPPINGS/VENDORS/MICSFT/WINDOWS/CP1252.TXT
It’s a plain text file in easy-to-process format, so if you don’t find an existing tool for the conversion, it should be rather straightforward to write mapping based on that file.
The file is indirectly cited in the official IANA registry:
http://www.iana.org/assignments/character-sets
我终于找到了解决办法。看起来只有从 128 到 159 的字节需要代理对。我使用这段代码来模拟 .NET Unicode 编码:
I finally found a solution. It looks like that only bytes from 128 to 159 need the surrogate pairs. I use this piece of code to emulate .NET Unicode encoding: