[英]How can I print a byte string of an integer to a socket in Java?
Using Java 8 for Android development. 使用Java 8进行Android开发。
How can I print an integer in base256 to a socket? 如何将base256中的整数打印到套接字? This code I've been trying for some reason prints 5 bytes... 我出于某种原因一直尝试的这段代码可以打印5个字节...
PrintWriter out = new PrintWriter(
new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
int FileSize = 430200;
byte[] BytFileSize = ByteBuffer.allocate(4).putInt(FileSize).array();
String StrFileSize = new String(BytFileSize, "ISO-8859-1");
out.print(StrFileSize);
out.flush();
It prints: 它打印:
"00 06 c2 90 78"
The expected is: 预期为:
"00 06 90 78"
Weird that it puts a "c2" in between the two 16-bit pieces... 奇怪的是,它在两个16位元之间插入了一个“ c2”。
This works: 这有效:
OutputStream out = socket.getOutputStream();
int FileSize = 430200;
byte[] BytFileSize = ByteBuffer.allocate(4).putInt(FileSize).array();
out.write(BytFileSize);
out.flush();
But I'm not sure why the PrintWriter(BufferedWriter(OutputStreamWriter(OutputStream))).print(String) version doesn't work correctly. 但我不确定为什么PrintWriter(BufferedWriter(OutputStreamWriter(OutputStream)))。print(String)版本无法正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.