简体   繁体   English

如何在Java中为幻数设置4字节位模式?

[英]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大小)中, -890xA9都具有相同的位模式: 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.

相关问题 如何将数字(作为字符串)转换为Java中的字节数组? - How do I convert a number (as a String) to an array of bytes in Java? 如何在java中生成160位素数? - How do I generate a 160 bit prime number in java? 如何读取.zip文件的前两个字节以确认是否存在适当的幻数? - How can I read the first two bytes of a .zip file to confirm that the appropriate magic number is present? 如何在Java中声明字节数组? - How do I declare an array of bytes in Java? UTF-8 编码后,如何截断 java 字符串以适应给定的字节数? - How do I truncate a java string to fit in a given number of bytes, once UTF-8 encoded? 如何截断 java integer 以适应/扩展到给定的字节数? - How do I truncate a java integer to fit/exand to a given number of bytes? 如何从java中的文件中获取幻数 - How to get the magic number from File in java 如何确定Java中文件每一行的字节数? - How can I determine the number of bytes of each line of a file in java? 如何在Java中存储无符号字节的存储(数组)以对其进行位操作? - how to store store (an array of) unsigned bytes in Java to do bit manipulations on them? 如何确定表示给定整数所需的字节数? - How do I determine number of bytes needed to represent a given integer?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM