[英]How do I set 4 bytes bit pattern for magic number in Java?
I am working on a network programming on linux machine using C++ and I was wondering how I can set 4 bytes bit pattern for magic number in Java (client). 我正在使用C ++在linux机器上进行网络编程,我想知道如何在Java(客户端)中为魔术数设置4字节位模式。 Also how do I set the same bit pattern in c++ co I can compare the one from client with the one on the server side. 另外我如何在c ++中设置相同的位模式我可以将客户端与服务器端的位模式进行比较。
Thank in advance.. 预先感谢..
Edit 编辑
So now I have this 所以现在我有了这个
byte[] signature = new byte[4];
for(int i=0; i<4; i++){
signature[i] = (byte) 0xA9;
}
And if I looked at the inside of the array after for loop from the debugger then I have 如果我在调试器的for循环之后查看数组的内部,那么我有
{-89, -89, -89, -89}
And I did something like this in C++ 我在C ++中做过类似的事情
uint8_t m_magicNumberBuffer[4];
magicKeyRead = read(m_fd, m_magicNumberBuffer, SIZE_OF_HEADER);
if(m_magicNumberBuffer[0] == 0xA9 && m_magicNumberBuffer[1] == 0xA9 && m_magicNumberBuffer[2] == 0xA9 && m_magicNumberBuffer[3] == 0xA9){
printf("SocketClient::recvMagicKey, Magic key has been found \n");
break;
}
I somehow works but not sure that I have declared m_magicNumberBuffer and unsigned integer but those were in negative 89 in java. 我以某种方式工作,但不确定我已经声明了m_magicNumberBuffer和无符号整数,但是在java中它们是负89。 Is this ok to do this in this way? 这样做可以吗?
Thanks in advance. 提前致谢。
Java has bitwise operators, for example bitwise OR |
Java具有按位运算符,例如按位OR |
, bitwise AND &
and bit shift operators >>>
, >>
and <<
, very similar to what C++ has. ,按位AND &
和位移运算符>>>
, >>
和<<
,与C ++非常相似。 You can use those to manipulate bits exactly as you want. 您可以使用它们完全按照您的意愿操作位。
Since you don't explain in more detail what you want to do, I cannot give you a more detailed answer. 由于你没有详细解释你想做什么,我不能给你一个更详细的答案。
In Java, you would represent it as 在Java中,您可以将其表示为
byte[] signature=new byte[4];
in C++, it would be 在C ++中,它会是
uint8_t signature[4];
You can then access each of the bytes individually as elements of the array. 然后,您可以单独访问每个字节作为数组的元素。
Both languages support hex codes, so for example, you could do 两种语言都支持十六进制代码,例如,您可以这样做
signature[0]=0xA9;
in either java or C++ and it will set the first bit to A9 in hexadecimal (which is 10101001 in binary) 在java或C ++中,它将第一位设置为十六进制的A9(二进制为10101001)
When you write to a DataOutputStream you write 8-bit bytes (sign is not important) 当您写入DataOutputStream时,您写入8位字节(符号并不重要)
String filename = "text.dat";
DataOutputStream dos = new DataOutputStream(new FileOutputStream(filename));
for (int i = 0; i < 4; i++)
dos.write(0xA9);
dos.close();
DataInputStream dis = new DataInputStream(new FileInputStream(filename));
for (int i = 0; i < 4; i++)
System.out.println(Integer.toHexString(dis.readUnsignedByte()));
dis.close();
prints 版画
a9
a9
a9
a9
Java assumes a byte is signed by default, however its is just 8-bits of data and used correctly can be unsigned or mean whatever you want it to. Java假定默认情况下对一个字节进行了签名,但它只是8位数据并且正确使用可以是无符号的或意味着你想要它。
What you're really dealing with is four bytes of raw memory; 你真正要处理的是四个字节的原始内存; all you're concerned with is the bit pattern, not the numeric values. 所有你关心的是位模式,而不是数值。 In eight bits (the size of a byte
in Java), -89
and 0xA9
both have the same bit pattern: 10101001
. 在8位(Java中的byte
大小)中, -89
和0xA9
都具有相同的位模式: 10101001
。 Because byte
is signed in Java, dumping the value will show a negative value, which is rather counter intuitive, but Java doesn't have an eight bit unsigned type. 因为byte
是用Java签名的,所以转储该值将显示负值,这相当直观,但Java没有8位无符号类型。
(Technically, 0xA9
isn't representable in a byte
, and trying to put it in a signed char
in C++ is illegal. But Java doesn't care about such niceties.) (从技术上讲, 0xA9
在一个byte
是不可表示的,并且试图将它放在C ++中的signed char
中是非法的。但Java并不关心这些细节。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.