| | |
| | | return String.format("%04x", num).toUpperCase(); |
| | | } |
| | | |
| | | // 计算16进制对应的数值 |
| | | public static int GetHex(char ch) throws Exception { |
| | | if (ch >= '0' && ch <= '9') |
| | | return (int) (ch - '0'); |
| | | if (ch >= 'a' && ch <= 'f') |
| | | return (int) (ch - 'a' + 10); |
| | | if (ch >= 'A' && ch <= 'F') |
| | | return (int) (ch - 'A' + 10); |
| | | throw new Exception("error param"); |
| | | } |
| | | |
| | | // 计算幂 |
| | | public static int GetPower(int nValue, int nCount) throws Exception { |
| | | if (nCount < 0) |
| | | throw new Exception("nCount can't small than 1!"); |
| | | if (nCount == 0) |
| | | return 1; |
| | | int nSum = 1; |
| | | for (int i = 0; i < nCount; ++i) { |
| | | nSum = nSum * nValue; |
| | | } |
| | | return nSum; |
| | | } |
| | | |
| | | public static void main(String[] args) { |
| | | |
| | |
| | | System.out.println(d2); |
| | | |
| | | System.out.println(d1 + d2); |
| | | |
| | | |
| | | } |
| | | |
| | |
| | | public static String folatToHexString(Float value) { |
| | | return Integer.toHexString(Float.floatToIntBits(value)); |
| | | } |
| | | |
| | | /** |
| | | * int转16进制字符串 1位置 00 |
| | | * |
| | | * @param num |
| | | * @return |
| | | */ |
| | | public static String intToHexStr1(int num) { |
| | | // 需要使用2字节表示b |
| | | return String.format("%02x", num).toUpperCase(); |
| | | } |
| | | |
| | | 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; |
| | | } |
| | | |
| | | /** |
| | | * 将二进制转换为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; |
| | | } |
| | | |
| | | /** |
| | | * 二进制补码:取反口加1 |
| | | * @param str |
| | | * @return |
| | | */ |
| | | public static Integer twoToString(String str) { |
| | | String two = ""; |
| | | 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"; |
| | | } |
| | | } |
| | | return biannary2Decimal(two) + 1; |
| | | } |
| | | |
| | | public static String byteToHex(byte b) { |
| | | String hex = Integer.toHexString(b & 0xFF); |
| | | if (hex.length() < 2) { |
| | | hex = "0" + hex; |
| | | } |
| | | return hex; |
| | | } |
| | | } |