1

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.

4 Answers 4

3

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.

1

In Java, you would represent it as

byte[] signature=new byte[4];

in C++, it would be

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)

0

When you write to a DataOutputStream you write 8-bit bytes (sign is not important)

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.

0

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. 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.

(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.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.