把byte转化成2进制字符串

 1     public class ByteToBinary {  
 2         /** 
 3          * 把byte数组转化成2进制字符串 
 4          * @param bArr 
 5          * @return 
 6          */  
 7         public String getBinaryStrFromByteArr(byte[] bArr){  
 8             String result ="";  
 9             for(byte b:bArr ){  
10                 result += getBinaryStrFromByte(b);  
11             }  
12             return result;    
13         }  
14         /** 
15          * 把byte转化成2进制字符串 
16          * @param b 
17          * @return 
18          */  
19         public String getBinaryStrFromByte(byte b){  
20             String result ="";  
21             byte a = b; ;  
22             for (int i = 0; i < 8; i++){  
23                 byte c=a;  
24                 a=(byte)(a>>1);//每移一位如同将10进制数除以2并去掉余数。  
25                 a=(byte)(a<<1);  
26                 if(a==c){  
27                     result="0"+result;  
28                 }else{  
29                     result="1"+result;  
30                 }  
31                 a=(byte)(a>>1);  
32             }  
33             return result;  
34         }  
35           
36         /** 
37          * 把byte转化成2进制字符串 
38          * @param b 
39          * @return 
40          */  
41         public String getBinaryStrFromByte2(byte b){  
42             String result ="";  
43             byte a = b; ;  
44             for (int i = 0; i < 8; i++){  
45                 result = (a % 2) + result;  
46                 a=(byte)(a>>1);  
47             }  
48             return result;  
49         }  
50           
51         /** 
52          * 把byte转化成2进制字符串 
53          * @param b 
54          * @return 
55          */  
56         public String getBinaryStrFromByte3(byte b){  
57             String result ="";  
58             byte a = b; ;  
59             for (int i = 0; i < 8; i++){  
60                 result = (a % 2) + result;  
61                 a = (byte) (a/2);  
62             }  
63             return result;  
64         }  
65     }  

转至http://blog.csdn.net/geolo/article/details/6162385

转载于:https://www.cnblogs.com/crystalxu/p/4177582.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值