问题描述
我想转换一个字节值至二值进行数据传输。基本上,我在一个字节数组,其中10101100是一个单字节发送像二进制的AC(10101100)的值。我希望能够获得这个字节,并将其转换回10101100.截至目前,我没有成功可言真的不知道从哪里开始。任何帮助将是巨大的。
修改:对不起,我没有意识到,我忘了补充具体细节的所有困惑
基本上,我需要使用一个字节数组,通过套接字连接发送二进制值。我能做到这一点,但我不知道如何转换的价值观,使他们正确显示。下面是一个例子:
我需要发送的十六进制值ACDE48并能够跨preT回来。根据文档,我必须将它转换为二进制的方式如下:字节[] B = {} 10101100,11011110,01001000,其中阵列中的每个地方可以容纳2的值。然后,我需要将这些值转换回来后,他们被发送和接收。我不知道如何去这样做。
字符串toBinary(字节[]字节)
{
StringBuilder的SB =新的StringBuilder(bytes.length * Byte.SIZE);
的for(int i = 0; I< Byte.SIZE * bytes.length;我++)
sb.append((字节[I / Byte.SIZE]所述;&下;则i%Byte.SIZE&放大器; 0x80的)== 0'0':'1');
返回sb.toString();
}字节[] fromBinary(String s)将
{
INT SLEN = s.length();
字节[] = toReturn新的字节[(SLEN + Byte.SIZE - 1)/ Byte.SIZE]
焦炭℃;
的for(int i = 0; I< SLEN;我++)
如果((C = s.charAt(ⅰ))=='1')
toReturn [I / Byte.SIZE] =(字节)(toReturn [I / Byte.SIZE] |(0x80的>>>(ⅰ%Byte.SIZE)));
否则如果(C!='0')
抛出新抛出:IllegalArgumentException();
返回toReturn;
}
有一些简单的方法也处理这个问题(假设大端)。
的Integer.parseInt(十六进制,16);
的Integer.parseInt(二进制,2);
和
Integer.toHexString(字节).subString((Integer.SIZE - Byte.SIZE)/ 4);
Integer.toBinaryString(字节).substring(Integer.SIZE - Byte.SIZE);
I am trying to convert a byte value to binary for data transfer. Basically, I am sending a value like "AC" in binary ("10101100") in a byte array where "10101100" is a single byte. I want to be able to receive this byte and convert it back into "10101100." As of now I have no success at all dont really know where to begin. Any help would be great.
edit: sorry for all the confusion I didnt realize that I forgot to add specific details.
Basically I need to use a byte array to send binary values over a socket connection. I can do that but I dont know how to convert the values and make them appear correctly. Here is an example:
I need to send the hex values ACDE48 and be able to interpret it back. According to documentation, I must convert it to binary in the following way: byte [] b={10101100,11011110,01001000}, where each place in the array can hold 2 values. I then need to convert these values back after they are sent and received. I am not sure how to go about doing this.
String toBinary( byte[] bytes )
{
StringBuilder sb = new StringBuilder(bytes.length * Byte.SIZE);
for( int i = 0; i < Byte.SIZE * bytes.length; i++ )
sb.append((bytes[i / Byte.SIZE] << i % Byte.SIZE & 0x80) == 0 ? '0' : '1');
return sb.toString();
}
byte[] fromBinary( String s )
{
int sLen = s.length();
byte[] toReturn = new byte[(sLen + Byte.SIZE - 1) / Byte.SIZE];
char c;
for( int i = 0; i < sLen; i++ )
if( (c = s.charAt(i)) == '1' )
toReturn[i / Byte.SIZE] = (byte) (toReturn[i / Byte.SIZE] | (0x80 >>> (i % Byte.SIZE)));
else if ( c != '0' )
throw new IllegalArgumentException();
return toReturn;
}
There are some simpler ways to handle this also (assumes big endian).
Integer.parseInt(hex, 16);
Integer.parseInt(binary, 2);
and
Integer.toHexString(byte).subString((Integer.SIZE - Byte.SIZE) / 4);
Integer.toBinaryString(byte).substring(Integer.SIZE - Byte.SIZE);
这篇关于转换字节在Java二进制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!