标签:
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
标签:
原文地址:http://www.cnblogs.com/crystalxu/p/4177582.html