[英]Converting Byte Array to Int Array to send over java socket
Hi I am new to Java development and have the following problem. 嗨,我是Java开发的新手,有以下问题。
I construct a char array with the values {130,56,0,0,2,0,0,0}. 我用值{130,56,0,0,2,0,0,0}构造了一个char数组。 I then pass that values into a String using String.valueOf(), once that is done I convert the sting into a byte array using getBytes() function.
然后,我使用String.valueOf()将这些值传递到String中,一旦完成,我将使用getBytes()函数将字符串转换为字节数组。
I use a DataOutputStream writer to write the data to socket. 我使用DataOutputStream编写器将数据写入套接字。 My problem lies in the fact that on tcp level using wireshark to trace the data I am actuality sending c2 82 38 00 00 02 00 00 00 on the wire and not the original 130 56 0 0 2 0 0 0 .
我的问题在于以下事实:在tcp级别上,使用wireshark跟踪数据,我实际上是在网上发送c2 82 38 00 00 02 00 00 00,而不是原始的130 56 0 0 2 0 0 0。
Code snippet below 下面的代码段
public void run() {
System.out.println("Got a client !");
try {
// Accept Loop while connected
while (true) {
byte[] arrOutut = new byte[4096];
int count = clientSocket.getInputStream().read(arrOutut, 0, 4096);
String clientRequest = "";
System.out.println("packet size is " + count + "\n");
if (count != -1) {
for (int outputCount = 0; outputCount < count; outputCount++) {
char response = (char) arrOutut[outputCount];
clientRequest = clientRequest + response;
}
System.out.println("Got a clientrequest "+ clientRequest + "\n");
count = 0;
} else {
break;
}
char[] Map = new char[]{130,56,0,0,2,0,0,0};
String StringMsg = String.valueOf(Map, 0, Map.length);
byte[] data = StringMsg.getBytes();
byte[] asciidata = StringMsg.getBytes("ASCII");
System.out.println("Byte Data "+ data);
System.out.println("Byte Data ACII" + asciidata);
OutputStream dOut = clientSocket.getOutputStream();
DataOutputStream dos = new DataOutputStream(dOut);
int sendDataLength = data.length;
dos.write(data, 0, sendDataLength);
dos.flush();
}
clientSocket.close();
} catch (SocketTimeoutException e) {
System.out.println("Connection timed out");
} catch (IOException e) {
throw new RuntimeException(e);
}
}
Is there anyway I send 130 56 0 0 2 0 0 0 on the wire using java socket writer 反正我使用java socket writer在网上发送130 56 0 0 2 0 0 0
Say for argument sake the following is necessary 为争辩说,以下是必要的
String StringMsg = String.valueOf(Map, 0, Map.length);
byte[] data = StringMsg.getBytes();
A char
is not meant to store binary data . char
不用于存储二进制数据 。 Yes, in C, it stores a signed, 8 bit integral number; 是的,它在C中存储一个带符号的8位整数。 in Java, however, a
char
is a UTF16 code unit. 但是,在Java中,
char
是UTF16代码单元。
You want byte
here; 你想在这里
byte
; more specifically, you want a ByteBuffer
: 更具体地说,您需要一个
ByteBuffer
:
final ByteBuffer buf = ByteBuffer.allocate(8);
buf.put((byte) 130);
// etc etc
// send buf.array() on the wire; or use a SocketChannel and use buf directly
And yes, it is a pain to have to cast to a byte
each time, but then this is how it is :/ 是的,每次都必须强制转换为一个
byte
是一件很痛苦的事情,但是事实就是这样://
Also, a lot of people are confused by this (I was when I began with Java coming from C), but the fact that primitive types are unsigned does not matter at all; 同样,很多人对此感到困惑(我当时是从C语言开始使用Java的),但是原始类型是无符号的这一事实根本无关紧要; bits remain bits.
位保留位。 For instance, byte -128 (or
Byte.MIN_VALUE
) is 1111 1111
. 例如,字节-128(或
Byte.MIN_VALUE
)是1111 1111
。 Unless you have to perform arithmetic operations on numbers, the fact that a primitive is negative or not has no influence at all. 除非必须对数字执行算术运算,否则图元是否为负的事实根本没有影响。 And by the way, this is also why Java has two operators for left byte shifting,
>>
and >>>
(the second one does not "carry" the sign bit). 顺便说一句,这也是Java具有两个用于左字节移位的运算符
>>
和>>>
(第二个运算符不“携带”符号位)。
By the way, Java primitive types are big endian -- even if the underlying architecture isn't. 顺便说一下,Java基本类型是大端字节序的-即使底层体系结构不是。
you have to understand the difference between a hexadecimal A
and the character A
. 您必须了解十六进制
A
和字符A
之间的区别。 The Hex A refers to the binary value 1010
where as the Character A
does not. 十六进制A表示二进制值
1010
,而字符A
则不是。 With that in mind if you are gonna store your bytes just use a byte array or ByteBuffer
考虑到这一点,如果您要存储字节,只需使用字节数组或
ByteBuffer
I construct a char array with the values {130,56,0,0,2,0,0,0}.
我用值{130,56,0,0,2,0,0,0}构造了一个char数组。
Why? 为什么?
I then pass that values into a String using String.valueOf()
然后,我使用String.valueOf()将这些值传递给String。
Why? 为什么?
String
is not a container for binary data. String
不是二进制数据的容器。
once that is done I convert the sting into a byte array using getBytes() function.
一旦完成,我将使用getBytes()函数将字符串转换为字节数组。
Why? 为什么?
Why not construct the original data straight into a byte array and cut the whole palaver? 为什么不将原始数据直接构造为字节数组并剪切整个Palaver?
I use a DataOutputStream writer
我使用DataOutputStream编写器
DataOutputStream
is a stream, not a Writer.
DataOutputStream
是流,而不是Writer.
to write the data to socket.
将数据写入套接字。 My problem lies in the fact that on tcp level using wireshark to trace the data I am actuality sending c2 82 38 00 00 02 00 00 00 on the wire and not the original 130 56 0 0 2 0 0 0 .
我的问题在于以下事实:在tcp级别上,使用wireshark跟踪数据,我实际上是在网上发送c2 82 38 00 00 02 00 00 00,而不是原始的130 56 0 0 2 0 0 0。
Exactly as predicted. 完全符合预期。 So why not send the original data as a byte array, and cut out several error prone steps?
那么,为什么不将原始数据作为字节数组发送,并减少几个容易出错的步骤呢?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.