java - 片段说明字节数组到端口号

标签 java arrays byte

我有一个包含 6 个字节的字节数组,最后 2 个表示端口号,同时正在寻找一种方法将这些最后一个字节转换为一个端口号我遇到过这个片段,



            int port = 0;
        port |= peerList[i+4] & 0xFF;
        port <<= 8;
        port |= peerList[i+5] & 0xFF;

它可以工作,但我需要澄清一下它是如何工作的?

最佳答案

    int port = 0;                       // Start with zero
    port |= peerList[i+4] & 0xFF;       // Assign first byte to port using bitwise or.
    port <<= 8;                         // Shift the bits left by 8 (so the byte from before is on the correct position)
    port |= peerList[i+5] & 0xFF;       // Assign the second, LSB, byte to port.

关于java - 片段说明字节数组到端口号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1021463/

相关文章:

mysql - 如何获取 MySQL 数据库的真实大小?

java - 如何从字符串中获取类型?

c++ - 如何创建 std::array 包装器类

C# 套接字消息解压

c++ - 数组下标c++的无效类型 ‘int[unsigned int]’

arrays - C 中权重 [a]= 数据上的段错误;

python - 将 CSV 文件分割成相等的部分?

java - Eclipse 终止应用程序服务器和 GWT 开发模式的快捷键

java - UuidGenerator Bean 类型不匹配?

java - 将 GridLayout 添加到 GroupLayout