Source From Here
Question
I read the byte from a binary file, and stored in the byte array B. I use System.out.println(Integer.toBinaryString(B[i])). the problem is:
How-To
Use Integer#toBinaryString():
Or you can leverage exist library
flib:
Execution result:
Question
I read the byte from a binary file, and stored in the byte array B. I use System.out.println(Integer.toBinaryString(B[i])). the problem is:
How-To
Use Integer#toBinaryString():
- byte b1 = (byte) 129;
- String s1 = String.format("%8s", Integer.toBinaryString(b1 & 0xFF)).replace(' ', '0');
- System.out.println(s1); // 10000001
- byte b2 = (byte) 2;
- String s2 = String.format("%8s", Integer.toBinaryString(b2 & 0xFF)).replace(' ', '0');
- System.out.println(s2); // 00000010
- package test;
- import flib.util.HexByteKit;
- public class Test {
- public static void main(String[] args) {
- byte b1[] = {(byte) 129};
- System.out.printf("%s\n", HexByteKit.Byte2Bin(b1));
- byte b2[] = {(byte) 2};
- System.out.printf("%s\n", HexByteKit.Byte2Bin(b2));
- }
- }
沒有留言:
張貼留言