| | |
| | | |
| | | import java.math.BigInteger; |
| | | |
| | | import static org.apache.poi.hssf.util.HSSFColor.LIGHT_CORNFLOWER_BLUE.hexString; |
| | | |
| | | public class BytesUtil { |
| | | |
| | | public static String byteToHex(byte b) { |
| | |
| | | return rsBinStr; |
| | | } |
| | | |
| | | public static String hexString2binaryString(String hexString, int num) { |
| | | //16进制转10进制 |
| | | BigInteger sint = new BigInteger(hexString, num); |
| | | //10进制转2进制 |
| | | String str = sint.toString(2); |
| | | if(str.length() < num){ |
| | | for (int i = str.length(); i < num; i++) { |
| | | str = "0" + str; |
| | | } |
| | | } |
| | | return str; |
| | | } |
| | | |
| | | /** |
| | | * 二进制补码:取反口加1 |
| | | * @param str |
| | | * @return |
| | | */ |
| | | public static Integer twoToString(String str) { |
| | | String two = ""; |
| | | System.out.println(str); |
| | | String[] split = str.split(""); |
| | | for (int i = 0;i< split.length;i++){ |
| | | if("1".equals(split[i])){ |
| | | two += "0"; |
| | | }else if("0".equals(split[i])){ |
| | | two += "1"; |
| | | } |
| | | } |
| | | System.out.println(two); |
| | | return biannary2Decimal(two) + 1; |
| | | } |
| | | |
| | | /** |
| | | * 将二进制转换为10进制 |
| | | * @param binStr |
| | | * @return |
| | | */ |
| | | public static Integer biannary2Decimal(String binStr){ |
| | | |
| | | Integer sum = 0; |
| | | int len = binStr.length(); |
| | | |
| | | for (int i=1;i<=len;i++){ |
| | | //第i位 的数字为: |
| | | int dt = Integer.parseInt(binStr.substring(i-1,i)); |
| | | sum+=(int)Math.pow(2,len-i)*dt; |
| | | } |
| | | return sum; |
| | | } |
| | | |
| | | public static void main(String[] args) { |
| | | String s = hexString2binaryString("21E9", 16); |
| | | System.out.println(s); |
| | | } |
| | | |
| | | /** |
| | | * 不足width个字节宽度时,前面补0至width*8 |
| | | * |
| | |
| | | nSum = nSum * nValue; |
| | | } |
| | | return nSum; |
| | | } |
| | | |
| | | public static void main(String[] args) { |
| | | |
| | | String hex = "<END>"; |
| | | byte[] attr = hex.getBytes(); |
| | | |
| | | hex = bytesToString(attr); |
| | | |
| | | } |
| | | |
| | | /** |