Commit 4b936b099d093d3b75884b10d0954f13da2ed5f0
1 parent
91dbce60
海亮定制
Showing
1 changed file
with
26 additions
and
15 deletions
海亮定制.md
... | ... | @@ -836,23 +836,34 @@ public static String bytesToHexString(byte[] src) { |
836 | 836 | |
837 | 837 | ```java |
838 | 838 | |
839 | - System.out.println(bytesToHexString("李四".getBytes("GB2312"))); | |
840 | - | |
841 | - public static String bytesToHexString(byte[] src) { | |
842 | - StringBuilder stringBuilder = new StringBuilder(""); | |
843 | - if (src == null || src.length <= 0) { | |
844 | - return ""; | |
845 | - } | |
846 | - for (int i = 0; i < src.length; i++) { | |
847 | - int v = src[i] & 0xFF; | |
848 | - String hv = Integer.toHexString(v); | |
849 | - if (hv.length() < 2) { | |
850 | - stringBuilder.append(0); | |
839 | +System.out.println(new String(hex2Bytes(bytesToHexString("李四".getBytes("GB2312"))),"GB2312")); | |
840 | + | |
841 | +public static String bytesToHexString(byte[] src) { | |
842 | + StringBuilder stringBuilder = new StringBuilder(""); | |
843 | + if (src == null || src.length <= 0) { | |
844 | + return ""; | |
845 | + } | |
846 | + for (int i = 0; i < src.length; i++) { | |
847 | + int v = src[i] & 0xFF; | |
848 | + String hv = Integer.toHexString(v); | |
849 | + if (hv.length() < 2) { | |
850 | + stringBuilder.append(0); | |
851 | + } | |
852 | + stringBuilder.append(hv); | |
853 | + stringBuilder.append(""); | |
854 | + } | |
855 | + return stringBuilder.toString(); | |
856 | +} | |
857 | + | |
858 | + public static byte[] hex2Bytes(String hex){ | |
859 | + if(hex.length() % 2 == 0){ | |
860 | + byte[] ret = new byte[hex.length() / 2]; | |
861 | + for(int i = 0 ; i< hex.length() / 2 ; i++){ | |
862 | + ret[i] = (byte)Integer.parseInt(hex.substring(2*i,2*i+2),16); | |
851 | 863 | } |
852 | - stringBuilder.append(hv); | |
853 | - stringBuilder.append(""); | |
864 | + return ret; | |
854 | 865 | } |
855 | - return stringBuilder.toString(); | |
866 | + return null; | |
856 | 867 | } |
857 | 868 | ``` |
858 | 869 | ... | ... |