java 在java中将String类型的二进制数转换为位
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13694312/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Converting String type binary number to bit in java
提问by Dc Redwing
I have a question about converting String type binary number to bit and write in the txt file.
我有一个关于将字符串类型的二进制数转换为位并写入txt文件的问题。
For example we have String like "0101011" and want to convert to bit type "0101011" then write in to the file on the disk.
例如,我们有像“0101011”这样的字符串,并且想要转换为位类型“0101011”,然后写入磁盘上的文件。
I would like to know is there anyway to covert to string to bit..
我想知道无论如何可以隐蔽到字符串到位..
i was searching on the web they suggest to use bitarray but i am not sure
我在网上搜索他们建议使用 bitarray 但我不确定
thanks
谢谢
回答by Ted Hopp
Try this:
试试这个:
int value = Integer.parseInt("0101011", 2); // parse base 2
Then the bit pattern in value
will correspond to the binary interpretation of the string "0101011"
. You can then write value
out to a file as a byte
(assuming the string is no more than 8 binary digits).
那么位模式 invalue
将对应于 string 的二进制解释"0101011"
。然后,您可以value
将 a写出到文件中byte
(假设字符串不超过 8 个二进制数字)。
EDITYou could also use Byte.parseByte("0101011", 2);
. However, byte values in Java are always signed. If you tried to parse an 8-bit value with the 8thbit set (like "10010110"
, which is 150 decimal), you would get a NumberFormatException
because values above +127 do not fit in a byte
. If you don't need to handle bit patterns greater than "01111111"
, then Byte.parseByte
works just as well as Integer.parseInt
.
编辑您也可以使用Byte.parseByte("0101011", 2);
. 但是,Java 中的字节值始终是有符号的。如果你试图用8解析8位值个位设置(如"10010110"
,这是150十进制),你会得到一个NumberFormatException
因为上述+127值不在一个合适的byte
。如果您不需要处理大于 的位模式"01111111"
,则Byte.parseByte
与Integer.parseInt
.
Recall, though, that to write a byte to a file, you use OutputStream.write(int)
, which takes an int
(not byte
) value—even though it only writes one byte. Might as well go with an int
value to start with.
不过,回想一下,要将一个字节写入文件,您可以使用OutputStream.write(int)
,它接受一个int
(not byte
) 值——即使它只写入一个字节。不妨从一个int
值开始。
回答by Bhavik Ambani
You can try the below code to avoid overflows of the numbers.
您可以尝试以下代码以避免数字溢出。
long avoidOverflows = Long.parseLong("11000000000000000000000000000000", 2);
int thisShouldBeANegativeNumber = (int) avoidOverflows;
System.out.println("Currect value : " + avoidOverflows + " -> " + "Int value : " + thisShouldBeANegativeNumber);
you can see the output
你可以看到输出
Currect value : 3221225472 -> Int value : -1073741824
回答by Satyanarayana
//Converting String to Bytes
bytes[] cipherText= new String("0101011").getBytes()
//Converting bytes to Bits and Convert to String
StringBuilder sb = new StringBuilder(cipherText.length * Byte.SIZE);
for( int i = 0; i < Byte.SIZE * cipherText .length; i++ )
sb.append((cipherText [i / Byte.SIZE] << i % Byte.SIZE & 0x80) == 0 ? '0' : '1');
//Byte code of input in Stirn form
System.out.println("Bytecode="+sb.toString()); // some binary data
//Convert Byte To characters
String bin = sb.toString();
StringBuilder b = new StringBuilder();
int len = bin.length();
int i = 0;
while (i + 8 <= len) {
char c = convert(bin.substring(i, i+8));
i+=8;
b.append(c);
}
//String format of Binary data
System.out.println(b.toString());