wes*_*erg 3 java string binary byte
正如标题所说,我该怎么做?它很容易从字符串转换 - >字节 - >字符串二进制,但如何转换回来?以下是一个例子.输出为:'f'为二进制:01100110 294984
我在某处读到了我可以使用Integer.parseInt但显然并非如此:(或者我做错了什么?
谢谢, :)
public class main{
public static void main(String[] args) {
String s = "f";
byte[] bytes = s.getBytes();
StringBuilder binary = new StringBuilder();
for (byte b : bytes)
{
int val = b;
for (int i = 0; i < 8; i++)
{
binary.append((val & 128) == 0 ? 0 : 1);
val <<= 1;
}
binary.append(' ');
}
System.out.println("'" + s + "' to binary: " + binary);
System.out.println(Integer.parseInt("01100110", 2));
}
}
Run Code Online (Sandbox Code Playgroud)
ars*_*jii 11
您可以使用Byte.parseByte()2的基数:
byte b = Byte.parseByte(str, 2);
Run Code Online (Sandbox Code Playgroud)
使用你的例子:
System.out.println(Byte.parseByte("01100110", 2));
Run Code Online (Sandbox Code Playgroud)
102
| 归档时间: |
|
| 查看次数: |
25867 次 |
| 最近记录: |