{"id":189,"date":"2019-09-12T00:16:32","date_gmt":"2019-09-11T16:16:32","guid":{"rendered":"http:\/\/zechs.taipei\/?p=189"},"modified":"2019-09-12T00:16:34","modified_gmt":"2019-09-11T16:16:34","slug":"%e8%bd%89java-%e4%b8%adbyte-ascii-hex-int-short-%e5%ad%97%e4%b8%b2%e4%b9%8b%e9%96%93%e7%9a%84%e4%ba%92%e8%bd%89","status":"publish","type":"post","link":"https:\/\/zechs.taipei\/?p=189","title":{"rendered":"[\u8f49]java \u4e2dbyte Ascii Hex int short \u5b57\u4e32\u4e4b\u9593\u7684\u4e92\u8f49"},"content":{"rendered":"<p>\u539f\u6587\u7db2\u5740\uff1a <a href=\"https:\/\/blog.csdn.net\/jimbo_lee\/article\/details\/38419843\">https:\/\/blog.csdn.net\/jimbo_lee\/article\/details\/38419843<\/a> <\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import android.graphics.Bitmap;\nimport android.graphics.Matrix;\nimport java.io.UnsupportedEncodingException;\nimport java.util.Arrays;\n \npublic class StrUtil {\n\tpublic static int hexCharToInt(char c) {\n\t\tif ((c >= '0') &amp;&amp; (c &lt;= '9'))\n\t\t\treturn (c - '0');\n\t\tif ((c >= 'A') &amp;&amp; (c &lt;= 'F'))\n\t\t\treturn (c - 'A' + 10);\n\t\tif ((c >= 'a') &amp;&amp; (c &lt;= 'f'))\n\t\t\treturn (c - 'a' + 10);\n \n\t\tthrow new RuntimeException(\"invalid hex char '\" + c + \"'\");\n\t}\n \n\tpublic static String bytesToAscii(byte[] bytes, int offset, int dateLen) {\n\t\tif ((bytes == null) || (bytes.length == 0) || (offset &lt; 0) || (dateLen &lt;= 0)) {\n\t\t\treturn null;\n\t\t}\n\t\tif ((offset >= bytes.length) || (bytes.length - offset &lt; dateLen)) {\n\t\t\treturn null;\n\t\t}\n \n\t\tString asciiStr = null;\n\t\tbyte[] data = new byte[dateLen];\n\t\tSystem.arraycopy(bytes, offset, data, 0, dateLen);\n\t\ttry {\n\t\t\tasciiStr = new String(data, \"ISO8859-1\");\n\t\t} catch (UnsupportedEncodingException e) {\n\t\t}\n\t\treturn asciiStr;\n\t}\n \n\tpublic static String bytesToAscii(byte[] bytes, int dateLen) {\n\t\treturn bytesToAscii(bytes, 0, dateLen);\n\t}\n \n\tpublic static String bytesToAscii(byte[] bytes) {\n\t\treturn bytesToAscii(bytes, 0, bytes.length);\n\t}\n \n\tpublic static String bytesToHexString(byte[] bytes, int offset, int len) {\n\t\tif (bytes == null)\n\t\t\treturn \"null!\";\n \n\t\tStringBuilder ret = new StringBuilder(2 * len);\n \n\t\tfor (int i = 0; i &lt; len; ++i) {\n\t\t\tint b = 0xF &amp; bytes[(offset + i)] >> 4;\n\t\t\tret.append(\"0123456789abcdef\".charAt(b));\n\t\t\tb = 0xF &amp; bytes[(offset + i)];\n\t\t\tret.append(\"0123456789abcdef\".charAt(b));\n\t\t}\n \n\t\treturn ret.toString();\n\t}\n \n\tpublic static String bytesToHexString(byte[] bytes, int len) {\n\t\treturn ((bytes == null) ? \"null!\" : bytesToHexString(bytes, 0, len));\n\t}\n \n\tpublic static String bytesToHexString(byte[] bytes) {\n\t\treturn ((bytes == null) ? \"null!\" : bytesToHexString(bytes, bytes.length));\n\t}\n \n\tpublic static byte[] hexStringToBytes(String s) {\n\t\tif (s == null)\n\t\t\treturn null;\n \n\t\tint sz = s.length();\n\t\ttry {\n\t\t\tbyte[] ret = new byte[sz \/ 2];\n\t\t\tfor (int i = 0; i &lt; sz; i += 2) {\n\t\t\t\tret[(i \/ 2)] = (byte) (hexCharToInt(s.charAt(i)) &lt;&lt; 4 | hexCharToInt(s.charAt(i + 1)));\n\t\t\t}\n \n\t\t\treturn ret;\n\t\t} catch (RuntimeException re) {\n\t\t}\n\t\treturn null;\n\t}\n \n\tpublic static byte[] shortToBytesLe(short shortValue) {\n\t\tbyte[] bytes = new byte[2];\n \n\t\tfor (int i = 0; i &lt; bytes.length; ++i) {\n\t\t\tbytes[i] = (byte) (shortValue >> i * 8 &amp; 0xFF);\n\t\t}\n \n\t\treturn bytes;\n\t}\n \n\tpublic static byte[] shortToBytesBe(short shortValue) {\n\t\tbyte[] bytes = new byte[2];\n \n\t\tfor (int i = 0; i &lt; bytes.length; ++i) {\n\t\t\tbytes[(bytes.length - i - 1)] = (byte) (shortValue >> i * 8 &amp; 0xFF);\n\t\t}\n \n\t\treturn bytes;\n\t}\n \n\tpublic static byte[] intToBytesLe(int intValue) {\n\t\tbyte[] bytes = new byte[4];\n \n\t\tfor (int i = 0; i &lt; bytes.length; ++i) {\n\t\t\tbytes[i] = (byte) (intValue >> i * 8 &amp; 0xFF);\n\t\t}\n \n\t\treturn bytes;\n\t}\n \n\tpublic static byte[] intToBytesBe(int intValue) {\n\t\tbyte[] bytes = new byte[4];\n \n\t\tfor (int i = 0; i &lt; bytes.length; ++i) {\n\t\t\tbytes[(bytes.length - i - 1)] = (byte) (intValue >> i * 8 &amp; 0xFF);\n\t\t}\n \n\t\treturn bytes;\n\t}\n \n\tpublic static int bytesToIntLe(byte[] bytes) {\n\t\tif ((bytes == null) || (bytes.length > 4)) {\n\t\t\tthrow new RuntimeException(\"invalid arg\");\n\t\t}\n \n\t\tint ret = 0;\n \n\t\tfor (int i = 0; i &lt; bytes.length; ++i) {\n\t\t\tret += ((bytes[i] &amp; 0xFF) &lt;&lt; i * 8);\n\t\t}\n \n\t\treturn ret;\n\t}\n \n\tpublic static int bytesToIntLe(byte[] data, int start, int end) {\n\t\treturn bytesToIntLe(Arrays.copyOfRange(data, start, end));\n\t}\n \n\tpublic static int bytesToIntBe(byte[] bytes) {\n\t\tif ((bytes == null) || (bytes.length > 4)) {\n\t\t\tthrow new RuntimeException(\"invalid arg\");\n\t\t}\n \n\t\tint ret = 0;\n \n\t\tfor (int i = 0; i &lt; bytes.length; ++i) {\n\t\t\tret += ((bytes[i] &amp; 0xFF) &lt;&lt; (bytes.length - i - 1) * 8);\n\t\t}\n \n\t\treturn ret;\n\t}\n \n\tpublic static int bytesToIntBe(byte[] data, int start, int end) {\n\t\treturn bytesToIntBe(Arrays.copyOfRange(data, start, end));\n\t}\n \n\tpublic static int bytesToIntLe(byte b0, byte b1, byte b2, byte b3) {\n\t\tint ret = 0;\n \n\t\tret = b0 &amp; 0xFF;\n\t\tret += ((b1 &amp; 0xFF) &lt;&lt; 8);\n\t\tret += ((b2 &amp; 0xFF) &lt;&lt; 16);\n\t\tret += ((b3 &amp; 0xFF) &lt;&lt; 24);\n \n\t\treturn ret;\n\t}\n \n\tpublic static int bytesToIntBe(byte b0, byte b1, byte b2, byte b3) {\n\t\tint ret = 0;\n \n\t\tret = (b0 &amp; 0xFF) &lt;&lt; 24;\n\t\tret += ((b1 &amp; 0xFF) &lt;&lt; 16);\n\t\tret += ((b2 &amp; 0xFF) &lt;&lt; 8);\n\t\tret += (b3 &amp; 0xFF);\n \n\t\treturn ret;\n\t}\n \n\tpublic static void byteArraySetByte(byte[] bytesArray, byte setValue, int index) {\n\t\tbytesArray[index] = setValue;\n\t}\n \n\tpublic static void byteArraySetByte(byte[] bytesArray, int setValue, int index) {\n\t\tbytesArray[index] = (byte) (setValue &amp; 0xFF);\n\t}\n \n\tpublic static void byteArraySetBytes(byte[] bytesArray, byte[] setValues, int index) {\n\t\tSystem.arraycopy(setValues, 0, bytesArray, index, setValues.length);\n\t}\n \n\tpublic static void byteArraySetWord(byte[] bytesArray, int setValue, int index) {\n\t\tbytesArray[index] = (byte) (setValue &amp; 0xFF);\n\t\tbytesArray[(index + 1)] = (byte) (setValue >> 8 &amp; 0xFF);\n\t}\n \n\tpublic static void byteArraySetWordBe(byte[] bytesArray, int setValue, int index) {\n\t\tbytesArray[index] = (byte) (setValue >> 8 &amp; 0xFF);\n\t\tbytesArray[(index + 1)] = (byte) (setValue &amp; 0xFF);\n\t}\n \n\tpublic static void byteArraySetInt(byte[] bytesArray, int setValue, int index) {\n\t\tbytesArray[index] = (byte) (setValue &amp; 0xFF);\n\t\tbytesArray[(index + 1)] = (byte) (setValue >> 8 &amp; 0xFF);\n\t\tbytesArray[(index + 2)] = (byte) (setValue >> 16 &amp; 0xFF);\n\t\tbytesArray[(index + 3)] = (byte) (setValue >> 24 &amp; 0xFF);\n\t}\n \n\tpublic static void byteArraySetIntBe(byte[] bytesArray, int setValue, int index) {\n\t\tbytesArray[index] = (byte) (setValue >> 24 &amp; 0xFF);\n\t\tbytesArray[(index + 1)] = (byte) (setValue >> 16 &amp; 0xFF);\n\t\tbytesArray[(index + 2)] = (byte) (setValue >> 8 &amp; 0xFF);\n\t\tbytesArray[(index + 3)] = (byte) (setValue &amp; 0xFF);\n\t}\n \n\tpublic static void delayms(int ms) {\n\t\tif (ms &lt;= 0)\n\t\t\treturn;\n\t\ttry {\n\t\t\tThread.sleep(ms);\n\t\t} catch (InterruptedException e) {\n\t\t}\n\t}\n \n\tpublic static Bitmap bitmapRotate(Bitmap b, int degrees) {\n\t\tif ((degrees != 0) &amp;&amp; (b != null)) {\n\t\t\tMatrix m = new Matrix();\n\t\t\tm.setRotate(degrees, b.getWidth() \/ 2.0F, b.getHeight() \/ 2.0F);\n\t\t\ttry {\n\t\t\t\tBitmap b2 = Bitmap.createBitmap(b, 0, 0, b.getWidth(), b.getHeight(), m, true);\n \n\t\t\t\tif (b != b2) {\n\t\t\t\t\tb.recycle();\n\t\t\t\t\tb = b2;\n\t\t\t\t}\n\t\t\t} catch (OutOfMemoryError ex) {\n\t\t\t}\n\t\t}\n\t\treturn b;\n\t}\n \n\tpublic static boolean isAscii(char ch) {\n\t\treturn (ch &lt;= '');\n\t}\n \n\tpublic static boolean isAscii(String text) {\n\t\tfor (int i = 0; i &lt; text.length(); ++i) {\n\t\t\tif (!(isAscii(text.charAt(i)))) {\n\t\t\t\treturn false;\n\t\t\t}\n\t\t}\n\t\treturn true;\n\t}\n \n\tpublic static boolean hasAsciiChar(String text) {\n\t\tfor (int i = 0; i &lt; text.length(); ++i) {\n\t\t\tif (isAscii(text.charAt(i))) {\n\t\t\t\treturn true;\n\t\t\t}\n\t\t}\n\t\treturn false;\n\t}\n}<\/pre>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import java.io.ByteArrayOutputStream;\n \nimport javax.crypto.Cipher;\nimport javax.crypto.SecretKey;\nimport javax.crypto.SecretKeyFactory;\nimport javax.crypto.spec.DESKeySpec;\nimport javax.crypto.spec.DESedeKeySpec;\n \n\/**\n * \u5b57\u8282\u3001\u6574\u6570\u3001\u957f\u6574\u6570\u7b49\u6570\u636e\u7c7b\u578b\u4e4b\u95f4\u7684\u62c6\u5206\u548c\u8f6c\u6362\u3002&lt;br\/>\n * \u8fd8\u5305\u62ecBCD\u7801\uff0c\u5341\u516d\u8fdb\u5236\u7801\uff0c\u7f51\u7edc\u5b57\u8282\u5e8f\u7b49\u7b49\u76f8\u4e92\u8f6c\u6362\n *\n * @author \n * @version 1.0\n *\/\npublic class TypeConvert\n{\n    \/**\n     * \u5b57\u8282\u6570\u7ec4\u8f6c\u6362\u6210\u6574\u578b\u3002(\u7f51\u7edc\u5b57\u8282\u5e8f\uff0c\u9ad8\u5b57\u8282\u5728\u524d)\n     *\n     * @param b \u5b57\u8282\u6570\u7ec4\u3002\n     * @param offset \u5f85\u8f6c\u6362\u5b57\u8282\u5f00\u59cb\u7684\u4f4d\u7f6e\u3002\n     * @return \u6574\u6570\u5f62\u5f0f\u3002\n     *\/\n    public final static int byte2int(byte[] b, int offset)\n    {\n        return (b[offset + 3] &amp; 0xff) | ((b[offset + 2] &amp; 0xff) &lt;&lt; 8) | ((b[offset + 1] &amp; 0xff) &lt;&lt; 16)\n                | ((b[offset] &amp; 0xff) &lt;&lt; 24);\n    }\n \n    \/**\n     * \u5b57\u8282\u6570\u7ec4\u8f6c\u6362\u6210\u6574\u578b\u3002(\u7f51\u7edc\u5b57\u8282\u5e8f\uff0c\u9ad8\u5b57\u8282\u5728\u524d)\n     *\n     * @param b \u5b57\u8282\u6570\u7ec4\u3002\n     * @return \u6574\u6570\u5f62\u5f0f\u3002\n     *\/\n    public final static int byte2int(byte[] b)\n    {\n        return (b[3] &amp; 0xff) | ((b[2] &amp; 0xff) &lt;&lt; 8) | ((b[1] &amp; 0xff) &lt;&lt; 16) | ((b[0] &amp; 0xff) &lt;&lt; 24);\n    }\n \n    \/**\n     * \u4f4e\u5b57\u8282\u5728\u524d\uff0c\u9ad8\u5b57\u8282\u5728\u540e\u5b57\u8282\u6d41\u8f6c\u6362\u4e3a\u6574\u578b\n     *\n     * @param buf byte[]\n     * @param offset int\n     * @return int\n     *\/\n    public final static int Sbyte2int(byte[] buf, int offset)\n    { \/\/ \u8bfb\u53d64\u4e2a.\n        return (buf[offset] &amp; 0xff) | ((buf[offset + 1] &lt;&lt; 8) &amp; 0xff00) | (((buf[offset + 2] &lt;&lt; 16) &amp; 0xff0000))\n                | (((buf[offset + 3] &lt;&lt; 24) &amp; 0xff000000));\n    }\n \n    \/**\n     * \u4f4e\u5b57\u8282\u5728\u524d\uff0c\u9ad8\u5b57\u8282\u5728\u540e\u6574\u578b\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\n     *\n     * @param n int\n     * @param buf byte[]\n     * @param offset int\n     *\/\n    public final static void Sint2byte(int n, byte[] buf, int offset)\n    {\n        buf[offset + 3] = (byte) (n >> 24);\n        buf[offset + 2] = (byte) (n >> 16);\n        buf[offset + 1] = (byte) (n >> 8);\n        buf[offset + 0] = (byte) n;\n    }\n \n    \/**\n     * \u6574\u578b\u8f6c\u6362\u621016\u8fdb\u5236,,\u8d1f\u6570\u6700\u540e\u8981\u5728\u6700\u9ad8\u4f4d\u52a01\u5c31\u662f8\n     *\n     * @param nInt int\n     * @return 16\u8fdb\u5236\u5ea6\u5b57\u7b26\u4e32\u3002\n     *\/\n    public final static String int2hex(int nInt)\n    {\n        int n = nInt, tmp;\n        \/\/ \u4e0d\u65ad\u7684\u79fb\u52a8\uff0c4\u4e2a\u5206\u4e00\u5c0f\u6bb5\uff0c\uff0c\u79fb\u4f4d\u548c\u4e0e\u6216\u64cd\u4f5c\u3002\n        char[] bb = new char[8];\n        for (int i = 0; i &lt; 32; i += 4)\n        {\n            tmp = (int) ((n >> i) &amp; 0x0000000f);\n            tmp = getHex(tmp);\n            bb[7 - i \/ 4] = (char) tmp;\n        }\n        return new String(bb);\n    }\n \n    private final static String HEX = \"0123456789ABCDEF\";\n \n    public final static int hex2int(String hex)\n    {\n        int ret = 0;\n        for (int i = 0, len = hex.length(); i &lt; len; i++)\n        {\n            ret = (ret &lt;&lt; 8) + HEX.indexOf(hex.charAt(i));\n        }\n \n        return ret;\n    }\n \n    \/\/ \u9ed8\u8ba4\u8981\u6c42\u662f\u5927\u5199\u5341\u516d\u8fdb\u5236\uff0c\u5426\u5219\u51fa\u9519\n    public final static byte hex2Byte(char hex)\n    {\n        byte b = (byte) HEX.indexOf(hex);\n        return b;\n    }\n \n    \/**\n     * \u5c06\u5b57\u8282\u6570\u7ec4\u5185\u5bb9\u6309\u7167\u4e8c\u8fdb\u5236\u7801\u6253\u5370\u51fa\u6765\n     *\n     * @param buf byte[]\n     * @return String\n     *\/\n    public final static String byte2binary(byte[] buf)\n    {\n        int len = buf.length;\n        StringBuilder chars = new StringBuilder();\n        byte tmp;\n        int inch = 0;\n        int j;\n        for (int i = 0; i &lt; len; i++)\n        {\n            tmp = buf[i];\n            chars.append(' ');\n            for (j = 7; j >= 0; j--)\n            {\n                inch = ((tmp >> j) &amp; 0x01);\n                if (inch == 0)\n                {\n                    chars.append('0');\n                }\n                else\n                {\n                    chars.append('1');\n                }\n            }\n        }\n        return chars.substring(1);\n    }\n \n    \/**\n     * BCD\u7801\u8f6c\u4e3a10\u8fdb\u5236\u4e32(\u963f\u62c9\u4f2f\u6570\u636e)&lt;br\/>\n     * * BCD\u4e0e\u5341\u8fdb\u5236\u5b57\u7b26\u4e32\u7684\u8f6c\u6362.&lt;br\/>\n     * BCD\uff08Binary Coded Decimal\uff09\u662f\u7528\u4e8c\u8fdb\u5236\u7f16\u7801\u8868\u793a\u7684\u5341\u8fdb\u5236\u6570\uff0c&lt;br\/>\n     * \u5341\u8fdb\u5236\u6570\u91c7\u75280~9\u5341\u4e2a\u6570\u5b57\uff0c\u662f\u4eba\u4eec\u6700\u5e38\u7528\u7684\u3002\u5728\u8ba1\u7b97\u673a\u4e2d\uff0c\u540c\u4e00\u4e2a\u6570\u53ef\u4ee5\u7528\u4e24\u79cdBCD\u683c\u5f0f\u6765\u8868\u793a\uff1a&lt;br\/>\n     * \u2460\u538b\u7f29\u7684BCD\u7801 \u2461\u975e\u538b\u7f29\u7684BCD\u7801&lt;br\/>\n     * \u538b\u7f29\u7684BCD\u7801\uff1a&lt;br\/>\n     * \u538b\u7f29\u7684BCD\u7801\u75284\u4f4d\u4e8c\u8fdb\u5236\u6570\u8868\u793a\u4e00\u4e2a\u5341\u8fdb\u5236\u6570\u4f4d\uff0c\u6574\u4e2a\u5341\u8fdb\u5236\u6570\u7528\u4e00\u4e32BCD\u7801\u6765\u8868\u793a\u3002&lt;br\/>\n     * \u4f8b\u5982\uff0c\u5341\u8fdb\u5236\u657059\u8868\u793a\u6210\u538b\u7f29\u7684BCD\u7801\u4e3a0101 1001\uff0c\u5341\u8fdb\u5236\u65701946\u8868\u793a\u6210\u538b\u7f29\u7684BCD\u7801\u4e3a0001 1001 0100 0110\u3002&lt;br\/>\n     * \u975e\u538b\u7f29\u7684BCD\u7801\uff1a&lt;br\/>\n     * \u975e\u538b\u7f29\u7684BCD\u7801\u75288\u4f4d\u4e8c\u8fdb\u5236\u6570\u8868\u793a\u4e00\u4e2a\u5341\u8fdb\u5236\u6570\u4f4d\uff0c\u5176\u4e2d\u4f4e4\u4f4d\u662fBCD\u7801\uff0c\u9ad84\u4f4d\u662f0\u3002&lt;br\/>\n     * \u4f8b\u5982\uff0c\u5341\u8fdb\u5236\u657078\u8868\u793a\u6210\u538b\u7f29\u7684BCD\u7801\u4e3a0111 1000\u3002&lt;br\/>\n     * \u4ece\u952e\u76d8\u8f93\u5165\u6570\u636e\u65f6\uff0c\u8ba1\u7b97\u673a\u63a5\u6536\u7684\u662fASCII\u7801\uff0c\u8981\u5c06ASCII\u7801\u8868\u793a\u7684\u6570\u8f6c\u6362\u6210BCD\u7801\u662f\u5f88\u7b80\u5355\u7684\uff0c&lt;br\/>\n     * \u53ea\u8981\u628aASCII\u7801\u7684\u9ad84\u4f4d\u6e05\u96f6\u5373\u53ef\u3002\u53cd\u4e4b\uff0c\u5982\u679c\u8981\u628aBCD\u7801\u8f6c\u6362\u6210ASII\u7801\uff0c\u53ea\u8981\u628aBCD\u7801 \"\u6216|\"00110000\u5373\u53ef\u3002\n     *\n     * @param bytes BCD\u7801\n     * @return String 10\u8fdb\u5236\u4e32\n     *\/\n    public static String bcd2Str(byte[] bytes)\n    {\n        if (bytes.length == 0)\n        {\n            return \"\";\n        }\n \n        StringBuffer sb = new StringBuffer();\n        for (int i = 0; i &lt; bytes.length; i++)\n        {\n            int h = ((bytes[i] &amp; 0xff) >> 4) + 48;\n            sb.append((char) h);\n            int l = (bytes[i] &amp; 0x0f) + 48;\n            sb.append((char) l);\n        }\n        return sb.toString();\n    }\n \n    \/**\n     * 10\u8fdb\u5236\u4e32\u8f6c\u4e3aBCD\u7801&lt;br\/>\n     *\n     * @param asc 10\u8fdb\u5236\u4e32\n     * @return byte[] BCD\u7801\n     *\/\n    public static byte[] str2Bcd(String data)\n    {\n        if (data.length() == 0)\n        {\n            return new byte[0];\n        }\n \n        String str = data;\n        \/\/ \u5947\u6570\u4e2a\u6570\u5b57\u9700\u5de6\u8865\u96f6\n        if (str.length() % 2 != 0)\n        {\n            str = \"0\" + str;\n        }\n        ByteArrayOutputStream baos = new ByteArrayOutputStream();\n        char[] cs = str.toCharArray();\n        for (int i = 0; i &lt; cs.length; i += 2)\n        {\n            int high = cs[i] - 48;\n            int low = cs[i + 1] - 48;\n            baos.write(high &lt;&lt; 4 | low);\n        }\n        return baos.toByteArray();\n    }\n \n    \/**\n     * \u5c06\u5b57\u8282\u6570\u7ec4\u5185\u5bb9\u6309\u7167\u5341\u516d\u8fdb\u5236\u7801\u6253\u5370\u51fa\u6765\n     *\n     * @param buf byte[]\n     * @param split String \u5206\u9694\u7b26\n     * @return String\n     *\/\n    public final static String bytes2hex(byte[] buf, String split)\n    {\n        if ((split == null) || (split.length() == 0))\n        {\n            return bytes2hex(buf);\n        }\n \n        if ((buf == null) || (buf.length == 0))\n        {\n            return \"\";\n        }\n        StringBuilder chars = new StringBuilder();\n        for (byte tmp : buf)\n        {\n            chars.append(split);\n            int inch = ((tmp >> 4) &amp; 0x0F);\n            inch = getHex(inch);\n            chars.append((char) inch);\n \n            inch = (tmp &amp; 0x0F);\n            inch = getHex(inch);\n            chars.append((char) inch);\n        }\n \n        return chars.substring(split.length());\n    }\n \n    \/**\n     * \u5c06\u5b57\u8282\u6570\u7ec4\u5185\u5bb9\u6309\u7167\u5341\u516d\u8fdb\u5236\u7801\u6253\u5370\u51fa\u6765\n     *\n     * @param buf byte[]\n     * @param split String \u5206\u9694\u7b26\n     * @return String\n     *\/\n    public final static String bytes2hex(byte[] buf)\n    {\n        if ((buf == null) || (buf.length == 0))\n        {\n            return \"\";\n        }\n \n        StringBuilder chars = new StringBuilder();\n        for (byte b : buf)\n        {\n            int inch = ((b >> 4) &amp; 0x0F);\n            inch = getHex(inch);\n            chars.append((char) inch);\n \n            inch = (b &amp; 0x0F);\n            inch = getHex(inch);\n            chars.append((char) inch);\n        }\n \n        return chars.toString();\n    }\n \n    private static int getHex(int inch)\n    {\n        if (inch >= 10)\n        {\n            return inch + 55;\n        }\n        else\n        {\n            return inch + 48;\n        }\n    }\n \n    public static String byte2hex(byte b)\n    {\n        String hex = \"\";\n        int inch = ((b >> 4) &amp; 0x0F);\n        inch = getHex(inch);\n        hex += (char) inch;\n \n        inch = (b &amp; 0x0F);\n        inch = getHex(inch);\n        hex += (char) inch;\n        return hex;\n    }\n \n    \/**\n     * \u5c06\u5341\u516d\u8fdb\u5236\u7801\u5b57\u7b26\u4e32\u8f6c\u6362\u6210\u5b57\u8282\u6570\u7ec4\u5185\u5bb9\n     *\n     * @param str String\n     * @return String\n     *\/\n    public final static byte[] hex2bytes(String str)\n    {\n        str = str.toUpperCase();\n        int len = str.length() \/ 2;\n        byte[] buf = new byte[len];\n        int inch = 0;\n        for (int i = 0; i &lt; len; i++)\n        {\n            inch = str.charAt(i + i);\n            if (inch >= 65)\n            {\n                buf[i] = (byte) (((inch - 55) &lt;&lt; 4) &amp; 0xF0);\n            }\n            else\n            {\n                buf[i] = (byte) (((inch - 48) &lt;&lt; 4) &amp; 0xF0);\n            }\n            inch = str.charAt(i + i + 1);\n            if (inch >= 65)\n            {\n                buf[i] = (byte) (buf[i] | ((inch - 55) &amp; 0x0F));\n            }\n            else\n            {\n                buf[i] = (byte) (buf[i] | ((inch - 48) &amp; 0x0F));\n            }\n \n        }\n        return buf;\n    }\n \n    \/***\n     * \u6839\u636e\u5b9e\u9645\u957f\u5ea6\u8fd4\u56de,\u4e0d\u8865\u4efb\u4f55\u4e1c\u897f.\n     *\n     * @param aa int\n     * @return String \u4e00\u4e2a16\u8fdb\u5236\u7684\u5b57\u7b26\u4e32.\n     *\/\n    public final static String int2hexA(int aa)\n    {\n        char[] cc = new char[8];\n        int len = 0;\n        int tmp = 0;\n        for (int offset = 28; offset >= 0; offset -= 4)\n        {\n            tmp = ((aa >> offset) &amp; 0x0f);\n            if ((tmp == 0) &amp;&amp; (len == 0))\n            { \/\/ \u5f00\u5934\u662f0\u5219\u7701\u7565\u8c03.\n                continue;\n            }\n            else\n                tmp = getHex(tmp);\n            \/\/ \u9047\u5230\u6b63\u5e38\u5b57\u7b26.\n            \/\/ len++;\n            cc[len++] = (char) tmp;\n        }\n        return new String(cc, 0, len);\n    }\n \n    public final static void main(String[] args)\n    {\n       \/* System.out.println(TypeConvert.byte2binary(\"asdf\".getBytes()));\n        System.out.println(TypeConvert.bytes2hex(\"asdf\".getBytes(), \" \"));\n        byte[] buf = TypeConvert.hex2bytes(\"f1234f\");\n        System.out.println(TypeConvert.bytes2hex(buf, \"\"));\n        System.out.println(TypeConvert.int2hex(255).substring(6));*\/\n    \tbyte[] tmpbytes=binaryTobytes(\"010001000111010000000000000000000000010100000000000000000000000\");\n    \tSystem.out.println(bytes2hex(tmpbytes));\n    \tSystem.out.println(byte2binary(hex2bytes(\"A23A000002800000\")));\n    }\n \n    \/**\n     * \u5b57\u8282\u6570\u7ec4\u8f6c\u6362\u6210\u957f\u6574\u578b\u3002(\u7f51\u7edc\u5b57\u8282\u5e8f\uff0c\u9ad8\u5b57\u8282\u5728\u524d)\n     *\n     * @param b \u5b57\u8282\u6570\u7ec4\u3002\n     * @return \u957f\u6574\u6570\u5f62\u5f0f\u3002\n     *\/\n    public final static long byte2long(byte[] b)\n    {\n        return ((long) b[7] &amp; 0xff) | (((long) b[6] &amp; 0xff) &lt;&lt; 8) | (((long) b[5] &amp; 0xff) &lt;&lt; 16)\n                | (((long) b[4] &amp; 0xff) &lt;&lt; 24) | (((long) b[3] &amp; 0xff) &lt;&lt; 32) | (((long) b[2] &amp; 0xff) &lt;&lt; 40)\n                | (((long) b[1] &amp; 0xff) &lt;&lt; 48) | ((long) b[0] &lt;&lt; 56);\n    }\n \n    \/**\n     * \u5b57\u8282\u6570\u7ec4\u8f6c\u6362\u6210\u957f\u6574\u578b\u3002(\u7f51\u7edc\u5b57\u8282\u5e8f\uff0c\u9ad8\u5b57\u8282\u5728\u524d)\n     *\n     * @param b byte[] \u5b57\u8282\u6570\u7ec4\u3002\n     * @param offset int\n     * @return long \u957f\u6574\u6570\u5f62\u5f0f\u3002\n     *\/\n    public final static long byte2long(byte[] b, int offset)\n    {\n        return ((long) b[offset + 7] &amp; 0xff) | (((long) b[offset + 6] &amp; 0xff) &lt;&lt; 8)\n                | (((long) b[offset + 5] &amp; 0xff) &lt;&lt; 16) | (((long) b[offset + 4] &amp; 0xff) &lt;&lt; 24)\n                | (((long) b[offset + 3] &amp; 0xff) &lt;&lt; 32) | (((long) b[offset + 2] &amp; 0xff) &lt;&lt; 40)\n                | (((long) b[offset + 1] &amp; 0xff) &lt;&lt; 48) | ((long) b[offset] &lt;&lt; 56);\n    }\n \n    \/**\n     * \u6574\u578b\u8f6c\u6362\u6210\u5b57\u8282\u3002(\u7f51\u7edc\u5b57\u8282\u5e8f\uff0c\u9ad8\u5b57\u8282\u5728\u524d)\n     *\n     * @param n \u6574\u6570\u3002\n     * @return \u957f\u5ea6\u4e3a4\u7684\u5b57\u8282\u6570\u7ec4\u3002\n     *\/\n    public final static byte[] int2byte(int n)\n    {\n        byte[] b = new byte[4];\n        b[0] = (byte) (n >> 24);\n        b[1] = (byte) (n >> 16);\n        b[2] = (byte) (n >> 8);\n        b[3] = (byte) n;\n        return b;\n    }\n \n    \/**\n     * \u6574\u578b\u8f6c\u6362\u6210\u5b57\u8282\u3002(\u7f51\u7edc\u5b57\u8282\u5e8f\uff0c\u9ad8\u5b57\u8282\u5728\u524d)\n     *\n     * @param n \u6574\u6570\u3002\n     * @param buf \u5b58\u653e\u8f6c\u6362\u7ed3\u679c\u7684\u5b57\u8282\u6570\u7ec4\u3002\n     * @param offset \u5b58\u653e\u4f4d\u7f6e\u7684\u504f\u79fb\u5730\u5740\u3002\n     *\/\n    public final static void int2byte(int n, byte[] buf, int offset)\n    {\n        buf[offset] = (byte) (n >> 24);\n        buf[offset + 1] = (byte) (n >> 16);\n        buf[offset + 2] = (byte) (n >> 8);\n        buf[offset + 3] = (byte) n;\n    }\n \n    \/**\n     * \u77ed\u6574\u578b\u8f6c\u6362\u6210\u5b57\u8282\u3002(\u7f51\u7edc\u5b57\u8282\u5e8f\uff0c\u9ad8\u5b57\u8282\u5728\u524d)\n     *\n     * @param n \u6574\u6570\u3002\n     * @return \u957f\u5ea6\u4e3a4\u7684\u5b57\u8282\u6570\u7ec4\u3002\n     *\/\n    public final static byte[] short2byte(int n)\n    {\n        byte[] b = new byte[2];\n        b[0] = (byte) (n >> 8);\n        b[1] = (byte) n;\n        return b;\n    }\n \n    \/**\n     * \u77ed\u6574\u578b\u8f6c\u6362\u6210\u5b57\u8282\u3002(\u7f51\u7edc\u5b57\u8282\u5e8f\uff0c\u9ad8\u5b57\u8282\u5728\u524d)\n     *\n     * @param n \u6574\u6570\u3002\n     * @param buf \u5b58\u653e\u8f6c\u6362\u7ed3\u679c\u7684\u5b57\u8282\u6570\u7ec4\u3002\n     * @param offset \u5b58\u653e\u4f4d\u7f6e\u7684\u504f\u79fb\u5730\u5740\u3002\n     *\/\n    public final static void short2byte(int n, byte[] buf, int offset)\n    {\n        buf[offset] = (byte) (n >> 8);\n        buf[offset + 1] = (byte) n;\n    }\n \n    \/**\n     * \u957f\u6574\u578b\u8f6c\u6362\u6210\u5b57\u8282\u3002(\u7f51\u7edc\u5b57\u8282\u5e8f\uff0c\u9ad8\u5b57\u8282\u5728\u524d)\n     *\n     * @param n \u957f\u6574\u6570\u3002\n     * @return \u957f\u5ea6\u4e3a8\u7684\u5b57\u8282\u6570\u7ec4\u3002\n     *\/\n    public final static byte[] long2byte(long n)\n    {\n        byte[] b = new byte[8];\n        \/\/ b[0]=(byte)(n>>57); \/\/ comment by edong 20011203\n        b[0] = (byte) (n >> 56);\n        b[1] = (byte) (n >> 48);\n        b[2] = (byte) (n >> 40);\n        b[3] = (byte) (n >> 32);\n        b[4] = (byte) (n >> 24);\n        b[5] = (byte) (n >> 16);\n        b[6] = (byte) (n >> 8);\n        b[7] = (byte) n;\n        return b;\n    }\n \n    \/**\n     * \u957f\u6574\u578b\u8f6c\u6362\u6210\u5b57\u8282\u3002(\u7f51\u7edc\u5b57\u8282\u5e8f\uff0c\u9ad8\u5b57\u8282\u5728\u524d)\n     *\n     * @param n \u957f\u6574\u6570\u3002\n     * @param buf \u5b58\u653e\u8f6c\u6362\u7ed3\u679c\u7684\u5b57\u8282\u6570\u7ec4\u3002\n     * @param offset \u5b58\u653e\u4f4d\u7f6e\u7684\u504f\u79fb\u5730\u5740\u3002\n     *\/\n    public final static void long2byte(long n, byte[] buf, int offset)\n    {\n        \/\/ buf[offset]=(byte)(n>>57); \/\/ comment by edong\n        \/\/ 20011203\n        buf[offset] = (byte) (n >> 56);\n        buf[offset + 1] = (byte) (n >> 48);\n        buf[offset + 2] = (byte) (n >> 40);\n        buf[offset + 3] = (byte) (n >> 32);\n        buf[offset + 4] = (byte) (n >> 24);\n        buf[offset + 5] = (byte) (n >> 16);\n        buf[offset + 6] = (byte) (n >> 8);\n        buf[offset + 7] = (byte) n;\n    }\n \n    \/\/ **************\u4f4e\u5b57\u8282\u5728\u524d*******************\n \n    \/**\n     * \u957f\u6574\u578b\u8f6c\u6362\u6210\u5b57\u8282\u3002(\u7f51\u7edc\u5b57\u8282\u5e8f\uff0c\u9ad8\u5b57\u8282\u5728\u524d)\n     *\n     * @param n \u957f\u6574\u6570\u3002\n     * @param buf \u5b58\u653e\u8f6c\u6362\u7ed3\u679c\u7684\u5b57\u8282\u6570\u7ec4\u3002\n     * @param offset \u5b58\u653e\u4f4d\u7f6e\u7684\u504f\u79fb\u5730\u5740\u3002\n     * @author zhangyong 2004-05-27\n     *\/\n    public final static void Slong2byte(long n, byte[] buf, int offset)\n    {\n        buf[offset + 7] = (byte) (n >> 56);\n        buf[offset + 6] = (byte) (n >> 48);\n        buf[offset + 5] = (byte) (n >> 40);\n        buf[offset + 4] = (byte) (n >> 32);\n        buf[offset + 3] = (byte) (n >> 24);\n        buf[offset + 2] = (byte) (n >> 16);\n        buf[offset + 1] = (byte) (n >> 8);\n        buf[offset] = (byte) n;\n    }\n \n    \/**\n     * \u6574\u578b\u8f6c\u6362\u6210\u5b57\u8282\u3002(\u7f51\u7edc\u5b57\u8282\u5e8f\uff0c\u4f4e\u5b57\u8282\u5728\u524d)\n     *\n     * @param iSource int \u6574\u6570\u3002\n     * @param buf byte[] \u5b58\u653e\u8f6c\u6362\u7ed3\u679c\u7684\u5b57\u8282\u6570\u7ec4\u3002\n     * @param offset int \u5b58\u653e\u4f4d\u7f6e\u7684\u504f\u79fb\u5730\u5740\u3002\n     * @return byte[]\n     *\/\n    static public byte[] inttobyte(int iSource, byte[] buf, int offset)\n    {\n        buf[offset] = (byte) iSource;\n        buf[offset + 1] = (byte) (iSource >> 8);\n        buf[offset + 2] = (byte) (iSource >> 16);\n        buf[offset + 3] = (byte) (iSource >> 24);\n        return (buf);\n    }\n \n    \/**\n     * \u77ed\u6574\u6570\u8f6c\u6362\u6210\u5b57\u8282\u3002(\u7f51\u7edc\u5b57\u8282\u5e8f\uff0c\u4f4e\u5b57\u8282\u5728\u524d)\n     *\n     * @param n \u77ed\u6574\u6570\u3002\n     * @param buf \u5b58\u653e\u8f6c\u6362\u7ed3\u679c\u7684\u5b57\u8282\u6570\u7ec4\u3002\n     * @param offset \u5b58\u653e\u4f4d\u7f6e\u7684\u504f\u79fb\u5730\u5740\u3002\n     * @author zhangyong 2004-05-27\n     *\/\n \n    public final static void shorttobyte(int n, byte[] buf, int offset)\n    {\n        buf[offset] = (byte) n;\n        buf[offset + 1] = (byte) (n >> 8);\n    }\n \n    \/**\n     * \u77ed\u6574\u6570\u8f6c\u6362\u6210\u5b57\u8282\u3002(\u7f51\u7edc\u5b57\u8282\u5e8f\uff0c\u4f4e\u5b57\u8282\u5728\u524d)\n     *\n     * @param b byte[] \u5b58\u653e\u8f6c\u6362\u7ed3\u679c\u7684\u5b57\u8282\u6570\u7ec4\u3002\n     * @param offset int \u5b58\u653e\u4f4d\u7f6e\u7684\u504f\u79fb\u5730\u5740\u3002\n     * @return int\n     *\/\n    public final static int bytetoshort(byte[] b, int offset)\n    {\n        return (b[offset] &amp; 0xff) | ((b[offset + 1] &amp; 0xff) &lt;&lt; 8);\n    }\n \n    \/**\n     * \u5b57\u8282\u6570\u7ec4\u8f6c\u6362\u6210\u6574\u578b\u3002(\u7f51\u7edc\u5b57\u8282\u5e8f\uff0c\u4f4e\u5b57\u8282\u5728\u524d)\n     *\n     * @param b \u5b57\u8282\u6570\u7ec4\u3002\n     * @param offset \u5f85\u8f6c\u6362\u5b57\u8282\u5f00\u59cb\u7684\u4f4d\u7f6e\u3002\n     * @return \u6574\u6570\u5f62\u5f0f\u3002\n     *\/\n    public final static int bytetoint(byte[] b, int offset)\n    {\n        return (b[offset] &amp; 0xff) | ((b[offset + 1] &amp; 0xff) &lt;&lt; 8) | ((b[offset + 2] &amp; 0xff) &lt;&lt; 16)\n                | ((b[offset + 3] &amp; 0xff) &lt;&lt; 24);\n    }\n \n    \/**\n     * \u5b57\u8282\u6570\u7ec4\u8f6c\u6362\u6210\u6574\u578b\u3002(\u7f51\u7edc\u5b57\u8282\u5e8f\uff0c\u4f4e\u5b57\u8282\u5728\u524d)\n     *\n     * @param b \u5b57\u8282\u6570\u7ec4\u3002\n     * @return \u6574\u6570\u5f62\u5f0f\u3002\n     *\/\n    public final static int bytetoint(byte[] b)\n    {\n        return (b[0] &amp; 0xff) | ((b[1] &amp; 0xff) &lt;&lt; 8) | ((b[2] &amp; 0xff) &lt;&lt; 16) | ((b[3] &amp; 0xff) &lt;&lt; 24);\n    }\n \n    \/**\n     * \u5b57\u8282\u6570\u7ec4\u8f6c\u6362\u6210\u957f\u6574\u578b\u3002(\u7f51\u7edc\u5b57\u8282\u5e8f\uff0c\u4f4e\u5b57\u8282\u5728\u524d)\n     *\n     * @param b \u5b57\u8282\u6570\u7ec4\u3002\n     * @return \u957f\u6574\u6570\u5f62\u5f0f\u3002\n     *\/\n    public final static long bytetolong(byte[] b)\n    {\n        \/\/ System.out.println(\"typeconvert=\"+((int)b[7]));\n        \/\/ System.out.println(\"typeconvert\"+((int)b[6]));\n        \/\/ System.out.println(\"typeconvert\"+((int)b[5]));\n        \/\/ System.out.println(\"typeconvert\"+((int)b[4]));\n        \/\/ System.out.println(\"typeconvert\"+((int)b[3]));\n        \/\/ System.out.println(\"typeconvert\"+((int)b[2]));\n        \/\/ System.out.println(\"typeconvert\"+((int)b[1]));\n        \/\/ System.out.println(\"typeconvert\"+((int)b[0]));\n        return ((long) b[0] &amp; 0xff) | (((long) b[1] &amp; 0xff) &lt;&lt; 8) | (((long) b[2] &amp; 0xff) &lt;&lt; 16)\n                | (((long) b[3] &amp; 0xff) &lt;&lt; 24) | (((long) b[4] &amp; 0xff) &lt;&lt; 32) | (((long) b[5] &amp; 0xff) &lt;&lt; 40)\n                | (((long) b[6] &amp; 0xff) &lt;&lt; 48) | ((long) b[7] &lt;&lt; 56);\n    }\n \n    \/**\n     * \u5b57\u8282\u6570\u7ec4\u8f6c\u6362\u6210\u957f\u6574\u578b\u3002(\u7f51\u7edc\u5b57\u8282\u5e8f\uff0c\u9ad8\u5b57\u8282\u5728\u524d)\n     *\n     * @param b byte[] \u5b57\u8282\u6570\u7ec4\u3002\n     * @param offset int\n     * @return long \u957f\u6574\u6570\u5f62\u5f0f\u3002\n     *\/\n    public final static long bytetolong(byte[] b, int offset)\n    {\n        return ((long) b[offset] &amp; 0xff) | (((long) b[offset + 1] &amp; 0xff) &lt;&lt; 8) | (((long) b[offset + 2] &amp; 0xff) &lt;&lt; 16)\n                | (((long) b[offset + 3] &amp; 0xff) &lt;&lt; 24) | (((long) b[offset + 4] &amp; 0xff) &lt;&lt; 32)\n                | (((long) b[offset + 5] &amp; 0xff) &lt;&lt; 40) | (((long) b[offset + 6] &amp; 0xff) &lt;&lt; 48)\n                | ((long) b[offset + 7] &lt;&lt; 56);\n    }\n \n   \/* *\/\/**\n     * \u4e8c\u8fdb\u5236\u5b57\u7b26\u4e32\u8f6c\u4e3a\u5b57\u8282\u6570\u7ec4\n     *\/\/*\n    public final static byte[] binaryTobytes(String binary){\n        if(binary==null){\n        \treturn null;\n        }\n        while(binary.length()%8!=0){\n        \tbinary=binary+\"0\";\n        }\n        int byteLen=binary.length()\/8;\n        byte[] desBytes=new byte[byteLen];\n        String tmpstr=null;\n        int tmpint=0;\n        int offset=1;\n        for(int i=0 ;i&lt;byteLen;i++){\n        \ttmpstr=binary.substring(i*8,i*8+8);\n        \tSystem.out.println(\"\u8f6c\u6362\u4e4b\u524d\u7684\u4e8c\u8fdb\u5236\u5b57\u7b26\u4e32\uff1a\"+tmpstr);\n        \toffset=1;\n        \ttmpint=0;\n            for(int j=8;j>0;j--){\n            \tif(tmpstr.substring(j-1,j).equals(\"1\")){\n            \t\ttmpint=tmpint+offset;\n            \t}\n            \toffset=offset*2;\n            }\n            System.out.println(\"\u8f6c\u6362\u4e4b\u540e\u7684\u4e8c\u8fdb\u5236\u5b57\u7b26\u4e32\uff1a\"+Integer.toBinaryString(tmpint));\n            desBytes[i]=(byte)tmpint;\n \t    }\n        return desBytes;\n    }*\/\n \n    \/**\n     * \u4e8c\u8fdb\u5236\u5b57\u7b26\u4e32\u8f6c\u4e3a\u5b57\u8282\u6570\u7ec4\n     *\/\n    public final static byte[] binaryTobytes(String binary){\n        if(binary==null){\n        \treturn null;\n        }\n \n        while(binary.length()%8!=0){\n        \tbinary=binary+\"0\";\n        }\n        int byteLen=binary.length()\/8;\n        String s=null;\n        byte b;\n        ByteArrayOutputStream tmpOs=new ByteArrayOutputStream();\n        for (int i = 0; i &lt; byteLen;i++) {\n\t\t\ts = binary.substring(i*8, i*8 + 8);\n\t\t\tif (s.substring(0, 1).equals(\"1\")) {\n\t\t\t\ts = \"0\" + s.substring(1);\n\t\t\t\tb = Byte.valueOf(s, 2);\n\t\t\t\tb |= 128;\n\t\t\t} else {\n\t\t\t\tb = Byte.valueOf(s, 2);\n\t\t\t}\n\t\t\ttmpOs.write(b);\n\t\t}\n\t\treturn tmpOs.toByteArray();\n    }\n \n    \/**\n     * \u5c06\u5b57\u8282\u6570\u7ec4\u5185\u5bb9\u6309\u7167\u4e8c\u8fdb\u5236\u7801\u6253\u5370\u51fa\u6765\n     *\n     * @param buf byte[]\n     * @return String\n     *\/\n    public final static String bytes2binary(byte[] buf)\n    {\n        int len = buf.length;\n        StringBuilder chars = new StringBuilder();\n        byte tmp;\n        int inch = 0;\n        int j;\n        for (int i = 0; i &lt; len; i++)\n        {\n            tmp = buf[i];\n            \/\/chars.append(' ');\n            for (j = 7; j >= 0; j--)\n            {\n                inch = ((tmp >> j) &amp; 0x01);\n                if (inch == 0)\n                {\n                    chars.append('0');\n                }\n                else\n                {\n                    chars.append('1');\n                }\n            }\n        }\n        return chars.substring(0);\n    }\n \n    public static byte[] des(String keys, String value) throws Exception{\n    \tif(keys.length() == 32) {\n    \t\tString s = keys.substring(0,16);\n    \t\tkeys += s;\n    \t}\n    \tbyte[] key = TypeConvert.hex2bytes(keys);\n    \tbyte[] val = TypeConvert.hex2bytes(value);\n    \tif (key.length == 8) {\n\t\t\tDESKeySpec desKey;\n\t\t\tdesKey = new DESKeySpec(key);\n\t\t\tSecretKeyFactory keyFactory = SecretKeyFactory.getInstance(\"DES\");\n\t\t\tSecretKey sKey = keyFactory.generateSecret(desKey);\n\t\t\tCipher cipher = Cipher.getInstance(\"DES\/ECB\/NoPadding\");\n\t\t\tcipher.init(Cipher.ENCRYPT_MODE, sKey);\n\t\t\treturn cipher.doFinal(val);\n\t\t} else {\n\t\t\tDESedeKeySpec desKey;\n\t\t\tdesKey = new DESedeKeySpec(key);\n\t\t\tSecretKeyFactory keyFactory = SecretKeyFactory.getInstance(\"DESede\");\n\t\t\tSecretKey sKey = keyFactory.generateSecret(desKey);\n\t\t\tCipher cipher = Cipher.getInstance(\"DESede\/ECB\/NoPadding\");\n\t\t\tcipher.init(Cipher.ENCRYPT_MODE, sKey);\n\t\t\treturn cipher.doFinal(val);\n\t\t}\n    }\n    \/**\n     * \u654f\u611f\u4fe1\u606f\u52a0* \u5904\u7406\n     * @param oldStr\n     * @return string\n     *\/\n\tpublic static String coverString(String oldStr){\n\t\tif(oldStr == null || oldStr.length() &lt;= 0){\n\t\t\treturn oldStr;\n\t\t}else{\n\t\t   \tint oldLen= oldStr.length();\n\t\t   \tint secrLen = oldLen\/3 ;\n\t\t   \tString coverStr = oldStr.substring(oldLen\/3, secrLen + oldLen\/3);\n \n\t\t   \tStringBuffer str = new StringBuffer(\"\");\n\t\t   \tfor(int i=0;i&lt;coverStr.length();i++){\n\t\t   \t\tstr.append('*');\n\t\t   \t}\n \n\t\t\treturn oldStr.replace(coverStr, str);\n\t\t}\n\t}\n\t \/**\n     * \u654f\u611f\u4fe1\u606f\u52a0* \u5904\u7406\n     * @param oldStr\n     * @return string\n     *\/\n\tpublic static String coverAllString(String oldStr){\n\t\tif(oldStr == null || oldStr.length() &lt;= 0){\n\t\t\treturn oldStr;\n\t\t}else{\n\t\t\treturn oldStr.replaceAll(\".\", \"*\");\n\t\t}\n        }\n}<\/pre>","protected":false},"excerpt":{"rendered":"<p>\u539f\u6587\u7db2\u5740\uff1a https:\/\/blog.csdn.net\/jimbo_lee\/article\/details\/3 &#8230; <a title=\"[\u8f49]java \u4e2dbyte Ascii Hex int short \u5b57\u4e32\u4e4b\u9593\u7684\u4e92\u8f49\" class=\"read-more\" href=\"https:\/\/zechs.taipei\/?p=189\" aria-label=\"\u95b1\u8b80\u3008[\u8f49]java \u4e2dbyte Ascii Hex int short \u5b57\u4e32\u4e4b\u9593\u7684\u4e92\u8f49\u3009\u5168\u6587\">\u95b1\u8b80\u5168\u6587<\/a><\/p>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-189","post","type-post","status-publish","format-standard","hentry","category-tech"],"_links":{"self":[{"href":"https:\/\/zechs.taipei\/index.php?rest_route=\/wp\/v2\/posts\/189","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zechs.taipei\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zechs.taipei\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zechs.taipei\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zechs.taipei\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=189"}],"version-history":[{"count":5,"href":"https:\/\/zechs.taipei\/index.php?rest_route=\/wp\/v2\/posts\/189\/revisions"}],"predecessor-version":[{"id":194,"href":"https:\/\/zechs.taipei\/index.php?rest_route=\/wp\/v2\/posts\/189\/revisions\/194"}],"wp:attachment":[{"href":"https:\/\/zechs.taipei\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=189"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zechs.taipei\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=189"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zechs.taipei\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=189"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}