How to convert a byte to its binary string representation
How to convert a byte to its binary string representation
For example, the bits in a byte B
are 10000010
, how can I assign the bits to the string str
literally, that is, str = "10000010"
.
byte b1 = (byte) ;
String s1 = String.format("%8s", Integer.toBinaryString(b1 & 0xFF)).replace(' ', '');
System.out.println(s1); // byte b2 = (byte) ;
String s2 = String.format("%8s", Integer.toBinaryString(b2 & 0xFF)).replace(' ', '');
System.out.println(s2); //
public class BitsSetCount
{
public static void main(String[] args)
{
int send = ; System.out.println( "[Input] Integer value: " + send + "\n" );
BitsSetCount.countBits( send );
} private static void countBits(int i)
{
System.out.println( "Integer.toBinaryString: " + Integer.toBinaryString(i) );
System.out.println( "Integer.toHexString: " + Integer.toHexString(i) );
System.out.println( "Integer.bitCount: "+ Integer.bitCount(i) ); int d = i & 0xff000000;
int c = i & 0xff0000;
int b = i & 0xff00;
int a = i & 0xff; System.out.println( "\nByte 4th Hex Str: " + Integer.toHexString(d) );
System.out.println( "Byte 3rd Hex Str: " + Integer.toHexString(c) );
System.out.println( "Byte 2nd Hex Str: " + Integer.toHexString(b) );
System.out.println( "Byte 1st Hex Str: " + Integer.toHexString(a) ); int all = a+b+c+d;
System.out.println( "\n(1st + 2nd + 3rd + 4th (int(s)) as Integer.toHexString: " + Integer.toHexString(all) ); System.out.println("(1st + 2nd + 3rd + 4th (int(s)) == Integer.toHexString): " +
Integer.toHexString(all).equals(Integer.toHexString(i) ) ); System.out.println( "\nIndividual bits for each byte in a 4 byte int:"); /*
* Because we are sending the MSF bytes to a method
* which will work on a single byte and print some
* bits we are generalising the MSF bytes
* by making them all the same in terms of their position
* purely for the purpose of printing or analysis
*/
System.out.print(
getBits( (byte) (d >> ) ) + " " +
getBits( (byte) (c >> ) ) + " " +
getBits( (byte) (b >> ) ) + " " +
getBits( (byte) (a >> ) )
); } private static String getBits( byte inByte )
{
// Go through each bit with a mask
StringBuilder builder = new StringBuilder();
for ( int j = ; j < ; j++ )
{
// Shift each bit by 1 starting at zero shift
byte tmp = (byte) ( inByte >> j ); // Check byte with mask 00000001 for LSB
int expect1 = tmp & 0x01; builder.append(expect1);
}
return ( builder.reverse().toString() );
} }
public static String byteToString(byte b) {
byte[] masks = { -, , , , , , , };
StringBuilder builder = new StringBuilder();
for (byte m : masks) {
if ((b & m) == m) {
builder.append('');
} else {
builder.append('');
}
}
return builder.toString();
}
public static String getByteBinaryString(byte b) {
StringBuilder sb = new StringBuilder();
for (int i = ; i >= ; --i) {
sb.append(b >>> i & );
}
return sb.toString();
}
String byteToBinaryString(byte b){
StringBuilder binaryStringBuilder = new StringBuilder();
for(int i = ; i < ; i++)
binaryStringBuilder.append(((0x80 >>> i) & b) == ? '':'');
return binaryStringBuilder.toString();
}
How to convert a byte to its binary string representation的更多相关文章
- Convert a byte[] array to readable string format. This makes the "hex" readable!
/* * Java Bittorrent API as its name indicates is a JAVA API that implements the Bittorrent Protocol ...
- Write a program to convert decimal to 32-bit unsigned binary.
Write a program to convert decimal to 32-bit unsigned binary. Write a program to convert a 32-bit un ...
- Byte Array to Hexadecimal String
Lookup Text: 23,879.41 (20.8X faster) Sentence: 1.15 (23.9X faster) /// <summary> /// Hex stri ...
- Cannot convert value of type [java.lang.String] to required type [java.util.Date] for property 'xxx': no matching editors or conversion strategy found
今天在完成项目的时候遇到了下面的异常信息: 04-Aug-2014 15:49:27.894 SEVERE [http-apr-8080-exec-5] org.apache.catalina.cor ...
- Binary String Matching
问题 B: Binary String Matching 时间限制: 3 Sec 内存限制: 128 MB提交: 4 解决: 2[提交][状态][讨论版] 题目描述 Given two strin ...
- NYOJ之Binary String Matching
Binary String Matching 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 Given two strings A and B, whose a ...
- ACM Binary String Matching
Binary String Matching 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 Given two strings A and B, whose alp ...
- 解决spring mvc 上传报错,Field [] isn't an enum value,Failed to convert value of type 'java.lang.String[]' to required type '
没有选择附件,但是点击上传按钮的时候会报错. 之前不选择文件,直接上传空文件是可以的,后来不知道改了什么就不行了. 错误信息: -- :: [http--] TRACE org.springframe ...
- Binary String Matching(kmp+str)
Binary String Matching 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 Given two strings A and B, whose alp ...
随机推荐
- 01:MFC应用程序编程
一 MFC的发展 VC 1.0->VC 5.0->VC 6.0->VC2008 SP1)->VS2010 二 MFC基础 1 MFC 微软基础类库 采用类的方式,将Win32 ...
- requests下载文件并重新上传
import re import requests from io import BytesIO from django.core.files.uploadedfile import InMemory ...
- 以python代码解释fork系统调用
import os print('Process (%s) start...' % os.getpid()) # Only works on Unix/Linux/Mac: pid = os.fork ...
- Web安全测试-WebScarab
[功能] WebScarab是一个用来分析使用HTTP和HTTPS协议的应用程序框架.其原理很简单,WebScarab可以记录它检测到的会话内容(请求和应答),并允许使用者可以通过多种形式来查看记录. ...
- 【Linux系统编程应用】Linux音频编程基础(一)【转】
转自:https://blog.csdn.net/dengjin20104042056/article/details/52435290 一.数字音频 音频信号是一种连续变化的模拟信号,但计算机只能处 ...
- 基于ZedBoard的Webcam设计(一):USB摄像头(V4L2接口)的图片采集【转】
转自:http://www.cnblogs.com/surpassal/archive/2012/12/19/zed_webcam_lab1.html 一直想把USB摄像头接到Zedboard上,搭建 ...
- js async await 终极异步解决方案
既然有了promise 为什么还要有async await ? 当然是promise 也不是完美的异步解决方案,而 async await 的写法看起来更加简单且容易理解. 回顾 Promise Pr ...
- VS2017插件开发-项目右键菜单
1.创建自定义命令 2.更改.vsct中Group节点的id <Group guid="guidPublishOwinPackageCmdSet1" id="MyM ...
- Python 单例模式讲解
Python 单例模式讲解 本节内容: classmethod用途 单例模式方法一 类__new__方法讲解 单例模式方法二 前言: 使用单例方法的好处:对于一个类,多次实例化会产生多个对象,若使用单 ...
- 0行代码实现任意形状图片展示--android-anyshape
前言 在Android开发中, 我们经常会遇到一些场景, 需要以一些特殊的形状显示图片, 比如圆角矩形.圆形等等.关于如何绘制这类形状, 网上已经有很多的方案,比如自定义控件重写onDraw方法, 通 ...