rac*_*ana 2 java byte bit-manipulation bytearray 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;
Run Code Online (Sandbox Code Playgroud)
为了编写上面的代码,我从这个链接中得到了帮助。但是该值被存储在xByte[0]并且xByte[1]是不正确的。它给出了像
xByte[0] :-15
xByte[1] :0
Run Code Online (Sandbox Code Playgroud)
这是写入方式吗?请建议我进行更正以获得正确的字节值。
只需使用Apache Commons 中的BinaryCodec:
byte[] bytes = new BinaryCodec().toByteArray("1000000111010000");
Run Code Online (Sandbox Code Playgroud)
如果你想自己做这样的转换,你的代码需要一些更正。
您期望它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));
Run Code Online (Sandbox Code Playgroud)
'0' 和 '1' 的输出:
Character 0 has a code point numeric value of 48
Character 1 has a code point numeric value of 49
Run Code Online (Sandbox Code Playgroud)
运算符 '<<'将char 操作数转换为 int,因此这种移位会产生错误的结果,因为:
firstByte = (byte) (firstByte | (A.charAt(i) << i));
Run Code Online (Sandbox Code Playgroud)
是相同的
firstByte = (byte) (firstByte | ( (int)A.charAt(i) << i));
Run Code Online (Sandbox Code Playgroud)
对于 char '0' 与将值 48 向左移动相同:
firstByte = (byte) (firstByte | ( 48 << i));
Run Code Online (Sandbox Code Playgroud)
要将字符 '0' 或 '1' 转换为 0 或 1 数值,请使用 Character.getNumericValue(A.charAt(i)):
firstByte = (byte) (firstByte | ( Character.getNumericValue(A.charAt(i)) << i));
Run Code Online (Sandbox Code Playgroud)
按值移动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));
Run Code Online (Sandbox Code Playgroud)
输出:
FIRST byte value = -127, binary representation = 10000001
SECOND byte value = -48, binary representation = 11010000
Run Code Online (Sandbox Code Playgroud)
整个更正示例:
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));
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
22663 次 |
| 最近记录: |