[英]How to get 'original' bytes of a Java String when read from DataOutputStream.writeUTF()?
Currently I'm transferring a String across the network, using DataInput/OutputStream's. 目前,我正在使用DataInput / OutputStream在网络上传输字符串。 The String I am transferring needs to be converted into a byte array, to be decrypted.
我要传输的字符串需要转换为字节数组,然后才能解密。
However, since when the string was written using DataOutputStream.writeUTF("foobar"), its byte array contains encoded Java Modified UTF-8 data, which stuffs up the encryption process. 但是,由于使用DataOutputStream.writeUTF(“ foobar”)编写字符串时,其字节数组包含已编码的Java修改的UTF-8数据,从而使加密过程变得更加繁琐。
How can I get the original bytes from the Java modified UTF-8 String? 如何从Java修改的UTF-8字符串中获取原始字节?
Unicode has several variants, where s-with-^ can either be one character or two: s plus combining-^. Unicode有多种变体,其中s-with- ^可以是一个字符或两个字符:s加combining- ^。 Java has a Normalizer class to convert to one specific variant.
Java有一个Normalizer类可以转换为一个特定的变体。 See http://docs.oracle.com/javase/tutorial/i18n/text/normalizerapi.html or look immediately at the API.
请参阅http://docs.oracle.com/javase/tutorial/i18n/text/normalizerapi.html或立即查看API。
This requires that the original string adheres to one variant. 这要求原始字符串遵循一种变体。 One cannot take bytes and then interprete them as UTF-8, because there are illegal sequences.
一个人不能取字节,然后将其解释为UTF-8,因为存在非法序列。 This was done to prevent recognizing a wrong byte/character when in the middle of a byte sequence.
这样做是为了防止在字节序列中间出现错误的字节/字符。
String normalizedString = Normalizer.normalize(s, Normalizer.Form.NFD);
如果您使用http://docs.oracle.com/javase/1.4.2/docs/api/java/io/DataOutputStream.html#write(byte [ ],int,int)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.