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). 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.
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
{-89, -89, -89, -89}
And I did something like this in 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. Is this ok to do this in this way?
Thanks in advance.
Java has bitwise operators, for example bitwise OR
|, bitwise AND&and bit shift operators>>>,>>and<<, very similar to what C++ has. 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.