在 Java 中从字符串创建字节类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10093860/
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
Creating a byte type from a String in Java
提问by kakush
I have this string 11110000
, which represents a byte
.
我有这个字符串11110000
,它代表一个byte
.
I want to create a byte
from that string, but I can't find any example on-line.
我想byte
从该字符串创建一个,但我在网上找不到任何示例。
Can someone please help me?
有人可以帮帮我吗?
回答by dacongy
int value = Integer.parseInt(s, 2); // 2 for binary
int value = Integer.parseInt(s, 2); // 2 for binary
If you want byte type:
如果你想要字节类型:
byte value = Byte.parseByte(s, 2); // 2 for binary
byte value = Byte.parseByte(s, 2); // 2 for binary
回答by Matt Ball
Use Byte#parseByte()
.
回答by dash1e
Use
利用
byte b = (byte) Integer.parseInt("11111",2);
回答by Jerome
byte myByte = (byte)Integer.parseInt("11110000", 2);