java - 在java中设置字节值

标签 java byte arrays bit-manipulation bit

我想在 java 中将二进制字符串转换为字节数组。我已经编写了代码来设置二进制字符串中字节数组的每一位 String A = "1000000111010000"

        private byte firstByte;
    private byte secondByte;
        byte xByte = new byte[2];

        for(int i=0 ; i<A.length() ;i++){

            if(i<8){
                System.out.println(" i : "+i+" A.char[i] :"+A.charAt(i));
                firstByte = (byte) (firstByte | (A.charAt(i) << i));
            }else{
                System.out.println(" i : "+i+" A.char[i] :"+A.charAt(i));
                secondByte = (byte) (secondByte | (A.charAt(i) << i));
            }
        }
        xByte[0] = firstByte;
        xByte[1] = secondByte;

为了编写上面的代码,我得到了 this 的帮助关联 。 但是存储在 xByte[0]xByte[1] 中的值不正确。 它给出的值如

                   xByte[0] :-15
                   xByte[1] :0

这是写法吗?请建议我更正以获得正确的字节值。

最佳答案

只需使用 BinaryCodec来自 Apache Commons:

 byte[] bytes = new BinaryCodec().toByteArray("1000000111010000");

如果您想自己进行此类转换,您的代码需要进行一些更正。
您期望 A.charAt(i) 将返回数字 0 或 1,但实际上会返回 char“0”或“1”。 char 数据类型是单个 16 位 Unicode 字符,数值范围从 0 到 2^16,它的值正式称为 code points .
要打印代码点值,您需要将 char 转换为 int:

System.out.println("Character " + A.charAt(i) + " has a code point numeric value of " + (int)A.charAt(i));

“0”和“1”的输出:

Character 0 has a code point numeric value of 48
Character 1 has a code point numeric value of 49

运算符“<<”converts char 操作数转换为 int,因此这种转换会产生错误的结果,因为:

firstByte = (byte) (firstByte | (A.charAt(i) << i));

相同
firstByte = (byte) (firstByte | ( (int)A.charAt(i) << i));

对于 char '0' 与向左移动值 48 相同:

firstByte = (byte) (firstByte | ( 48 << i));

要将 char '0' 或 '1' 转换为 0 或 1 数值,请使用 Character.getNumericValue(A.charAt(i)):

firstByte = (byte) (firstByte | ( Character.getNumericValue(A.charAt(i)) << i));

按值 i 移动也是不正确的。您需要为第一个字节移动 (7-i) 或为第二个字节移动 (7-i%8)。当索引 i 达到 8 时需要从 0 开始计数,因此 i%8

打印字节类型的值时,您有两种选择:字节数值或二进制字符串表示形式:

System.out.println("FIRST  byte numeric value = " + xByte[0] + ", binary string representation = " + Integer.toBinaryString((xByte[0]+256)%256));
System.out.println("SECOND byte numeric  value = " + xByte[1] + ", binary string representation = " + Integer.toBinaryString((xByte[1]+256)%256));

输出:

FIRST  byte value = -127, binary representation = 10000001
SECOND byte value = -48, binary representation = 11010000

整个更正示例:

public class ByteTest
{

  public static void main(String[] args)
  {

    byte firstByte=0;
    byte secondByte=0;

    String A = "1000000111010000";
    byte[] xByte = new byte[2];





    for(int i=0 ; i<A.length() ;i++){

      System.out.println("Character " + A.charAt(i) + " has a code point numeric value of " + (int)A.charAt(i));

      if(i<8){
        System.out.println(" i : "+i+" A.char[i] :"+A.charAt(i));
        firstByte = (byte) (firstByte | (Character.getNumericValue(A.charAt(i)) << (7-i)));
      }else{
        System.out.println(" i : "+i+" A.char[i] :"+A.charAt(i));
        secondByte = (byte) (secondByte | (Character.getNumericValue(A.charAt(i)) << (7-i%8)));
      }
    }
    xByte[0] = firstByte;
    xByte[1] = secondByte;


    System.out.println("FIRST  byte numeric value = " + xByte[0] + ", binary string representation = " + Integer.toBinaryString((xByte[0]+256)%256));
    System.out.println("SECOND byte numeric  value = " + xByte[1] + ", binary string representation = " + Integer.toBinaryString((xByte[1]+256)%256));




  }

}

关于java - 在java中设置字节值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18998349/

相关文章:

java - jsoup 获取带有类的 div 元素

java - 以编程方式将 Xtend 类设置为 Java 类的父类(super class)

java - 我想将摩尔斯电码翻译成文字。还有一个小问题

java - 在 Java 字符(16 位)中存储 UTF-8 字符(8 位)时如何避免内存浪费。二合一?

c - 在 C 中将两个 8 位寄存器读入 ADXL362 的 12 位值

php - 按数组的一列从数据库查询中对数组进行排序

javascript - 为什么在这个 JavaScript 示例中移位比索引访问更快?

java - 在 Java 中将值插入到 TABLE

c++ - 字节变量值错误

php - 所有 WordPress 选项作为单个选项(序列化多维数组)还是多个选项?