简体   繁体   English

从DataOutputStream.writeUTF()读取时如何获取Java字符串的“原始”字节?

[英]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.

相关问题 为什么 DataOutputStream.writeUTF() 在开头添加额外的 2 个字节? - Why does DataOutputStream.writeUTF() add additional 2 bytes at the beginning? android socket DataOutputStream.writeUTF - android socket DataOutputStream.writeUTF 通过DataOutputStream.writeUTF()发送信息时出现奇怪的字符 - strange characters when sending information via DataOutputStream.writeUTF() java.io.DataOutputStream 中的 writeUTF - writeUTF in java.io.DataOutputStream 使用dataoutputstream将字节写入套接字时,如何读取datainputstream? - How to read a datainputstream when using a dataoutputstream to write bytes to socket? 从 swift 中修改后的 Java UTF(使用 java writeUTF() 方法创建)的前两个字节获取内容长度 - Get the content length from first two bytes of modified Java UTF (created using java writeUTF() method) in swift 在线程中循环 DataOutputStream writeUTF - Looping DataOutputStream writeUTF in thread 从DataOutputStream重新分配给BufferedOutputStream和FileOutputStream之后,再也无法使用writeUTF() - Not able to use writeUTF() anymore after reassigning to BufferedOutputStream and FileOutputStream from DataOutputStream Java如何将字节读入字符串 - Java how to read bytes into String Java将单个字节写入DataOutputStream - Java write individual bytes to DataOutputStream
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM