在java中将位串转换为字节

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20516636/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 02:29:10  来源:igfitidea点击:

Convert bitstring to byte in java

javabyte

提问by user2656572

I need to set each bit in 1 byte in Java.

我需要在 Java 中将每个位设置为 1 个字节。

bit7 -  1,
bit6 -  1,
bit5 - 1,
bit4 -  0,
bit3 – 0,
bit2 – 0,
bit1 – 0,
bit0 – 0

I've written:

我写过:

byte extra_dop = 0b00000111;

but got the following error:

但出现以下错误:

binary literals are not supported in -source 1.5 (use -source 7 or higher to enable binary literals)

-source 1.5 不支持二进制文字(使用 -source 7 或更高版本来启用二进制文字)

回答by Sean Patrick Floyd

That's not the way we do things in Java. Have a look at the BitSetclass, it's a much more convenient way of setting bit flags.

这不是我们在 Java 中做事的方式。看看这个BitSet类,它是一种更方便的设置位标志的方法。

Hmpfh. Let me rephrase that. Java is an Object Oriented language where there are efficient and Object Oriented ways of doing things in a more developer-friendly way than with bit operators. I'd suggest you use BitSet, as it makes your code much more readable, and it can support many more flags than a simple bit mask. Better?

哼哼。让我重新表述一下。Java 是一种面向对象的语言,与位运算符相比,Java 有高效且面向对象的方式来以对开发人员更友好的方式做事。我建议您使用BitSet,因为它使您的代码更具可读性,并且它可以支持比简单位掩码更多的标志。更好的?

回答by prmottajr

It depends on what you want to accomplish. If you just want to assign a value that won't be binary changed then what you are doing is just fine, but to use that functionallity you will have to compile specifying that javac will receive a source code that complies with java 7 (that is what the error messaging is saying). To do this depends on the way you are compiling, if you are using Netbeans or Eclipse then you will do this on the project properties configuration (just right click on the project and look for the properties, it will open a dialog, I don't remember right now where the source code compatibility is in each IDE, but I am almost sure it is right in the main screen of the dialog).

这取决于你想完成什么。如果您只想分配一个不会被二进制更改的值,那么您所做的就很好,但是要使用该功能,您必须编译指定 javac 将接收符合 java 7 的源代码(即错误消息在说什么)。要做到这一点取决于你的编译方式,如果你使用 Netbeans 或 Eclipse,那么你将在项目属性配置上执行此操作(只需右键单击项目并查找属性,它将打开一个对话框,我不这样做)现在不记得每个 IDE 中的源代码兼容性在哪里,但我几乎可以肯定它在对话框的主屏幕中是正确的)。

However if you want to edit the number later using bit operations then you will need to work like @Sean said using BitSet (actually you could also use bit operations directly on the numbers like we do in C/C++, it is just not confortable, but possible).

但是,如果您想稍后使用位操作编辑数字,那么您需要像@Sean 所说的使用 BitSet 一样工作(实际上您也可以像我们在 C/C++ 中那样直接对数字使用位操作,只是不舒服,但可能)。

回答by Amit G

Binary literal were introduced in Java7.

二进制文字是在 Java7 中引入的。

Use following for older version:

对旧版本使用以下内容:

byte b = Byte.parseByte("00000111", 2);

回答by Jesper

As the error message says, the 0b...syntax did not exist yet in Java 5 (which is what you seem to be using); it was introduced with Java 7. If you are using Java 7, make sure your compiler settings (in your IDE or build file) are set so that it accepts Java 7 syntax.

正如错误消息所说,0b...Java 5 中尚不存在该语法(您似乎正在使用该语法);它是在 Java 7 中引入的。如果您使用的是 Java 7,请确保您的编译器设置(在您的 IDE 或构建文件中)设置为它接受 Java 7 语法。

Bits are normally counted from the right to the left, so if you say bit 7 is 1, bit 6 is 1, etc. then I would expect the binary number to be 11100000instead of 00000111.

位通常从右到左计数,所以如果你说第 7 位是 1,第 6 位是 1,等等,那么我希望二进制数是11100000而不是00000111

To write this in source code in a Java version older than Java 7, you could simply write it as a hexadecimal or decimal number:

要在早于 Java 7 的 Java 版本的源代码中编写此代码,您可以简单地将其编写为十六进制或十进制数:

// Hexadecimal
byte extra_dop = (byte)0xE0; // or did you mean 0x07?

// Decimal
byte extra_dop = (byte)224; // or did you mean 7?

You could also use Integer.parseInt()with radix 2:

您还可以使用Integer.parseInt()基数 2:

byte extra_dop = (byte)Integer.parseInt("11100000", 2);

(Note, you could also use Byte.parseBytebut it will not accept 11100000since it exceeds the range of the signed bytetype).

(注意,您也可以使用Byte.parseByte但它不会接受,11100000因为它超出了签名byte类型的范围)。