{"id":852,"date":"2021-09-16T10:21:00","date_gmt":"2021-09-16T02:21:00","guid":{"rendered":"http:\/\/www.langmanezhuang.com\/index.php\/2021\/09\/16\/%e5%90%84%e7%a7%8d%e6%95%b0%e6%8d%ae%e7%b1%bb%e5%9e%8b%e8%bd%ac%e6%8d%a2%e4%b8%ba%e4%ba%8c%e8%bf%9b%e5%88%b6bytearrayconveter\/"},"modified":"2021-09-16T10:21:00","modified_gmt":"2021-09-16T02:21:00","slug":"%e5%90%84%e7%a7%8d%e6%95%b0%e6%8d%ae%e7%b1%bb%e5%9e%8b%e8%bd%ac%e6%8d%a2%e4%b8%ba%e4%ba%8c%e8%bf%9b%e5%88%b6bytearrayconveter","status":"publish","type":"post","link":"http:\/\/blog.langmanezhuang.com\/index.php\/2021\/09\/16\/%e5%90%84%e7%a7%8d%e6%95%b0%e6%8d%ae%e7%b1%bb%e5%9e%8b%e8%bd%ac%e6%8d%a2%e4%b8%ba%e4%ba%8c%e8%bf%9b%e5%88%b6bytearrayconveter\/","title":{"rendered":"\u5404\u79cd\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u4e3a\u4e8c\u8fdb\u5236ByteArrayConveter"},"content":{"rendered":"<div class=\"cnblogs_Highlighter\">\n<pre class=\"brush:java;gutter:true;\">package com.ioufev;\n\nimport java.nio.ByteOrder;\n\npublic class ByteArrayConveter {\n \n\t\/\/ char\u8f6c\u6362\u4e3abyte[2]\u6570\u7ec4\n\tpublic static byte[] getByteArray(char c) {\n\t\tbyte[] b = new byte[2];\n\t\tb[0] = (byte) ((c &amp; 0xff00) &gt;&gt; 8);\n\t\tb[1] = (byte) (c &amp; 0x00ff);\n\t\treturn b;\n\t}\n \n\t\/\/ \u4ecebyte\u6570\u7ec4\u7684index\u5904\u7684\u8fde\u7eed\u4e24\u4e2a\u5b57\u8282\u83b7\u5f97\u4e00\u4e2achar\n\tpublic static char getChar(byte[] arr, int index) {\n\t\treturn (char) (0xff00 &amp; arr[index] &lt;&lt; 8 | (0xff &amp; arr[index + 1]));\n\t}\n\t\/\/ short\u8f6c\u6362\u4e3abyte[2]\u6570\u7ec4\n\tpublic static byte[] getByteArray(short s) {\n\t\tbyte[] b = new byte[2];\n\t\tb[0] = (byte) ((s &amp; 0xff00) &gt;&gt; 8);\n\t\tb[1] = (byte) (s &amp; 0x00ff);\n\t\treturn b;\n\t}\n\t\/\/ \u4ecebyte\u6570\u7ec4\u7684index\u5904\u7684\u8fde\u7eed\u4e24\u4e2a\u5b57\u8282\u83b7\u5f97\u4e00\u4e2ashort\n\tpublic static short getShort(byte[] arr, int index) {\n\t\treturn (short) (0xff00 &amp; arr[index] &lt;&lt; 8 | (0xff &amp; arr[index + 1]));\n\t}\n\t\/\/ int\u8f6c\u6362\u4e3abyte[4]\u6570\u7ec4\n\tpublic static byte[] getByteArray(int i) {\n\t\tbyte[] b = new byte[4];\n\t\tb[0] = (byte) ((i &amp; 0xff000000) &gt;&gt; 24);\n\t\tb[1] = (byte) ((i &amp; 0x00ff0000) &gt;&gt; 16);\n\t\tb[2] = (byte) ((i &amp; 0x0000ff00) &gt;&gt; 8);\n\t\tb[3] = (byte)  (i &amp; 0x000000ff);\n\t\treturn b;\n\t}\n\t\/\/ \u4ecebyte\u6570\u7ec4\u7684index\u5904\u7684\u8fde\u7eed4\u4e2a\u5b57\u8282\u83b7\u5f97\u4e00\u4e2aint\n\tpublic static int getInt(byte[] arr, int index) {\n\t\treturn \t(0xff000000 \t&amp; (arr[index+0] &lt;&lt; 24))  | \n\t\t\t\t(0x00ff0000 \t&amp; (arr[index+1] &lt;&lt; 16))  | \n\t\t\t\t(0x0000ff00 \t&amp; (arr[index+2] &lt;&lt; 8))   | \n\t\t\t\t(0x000000ff \t&amp;  arr[index+3]);\n\t}\n\t\/\/ float\u8f6c\u6362\u4e3abyte[4]\u6570\u7ec4\n\tpublic static byte[] getByteArray(float f) {\n\t\tint intbits = Float.floatToIntBits(f);\/\/\u5c06float\u91cc\u9762\u7684\u4e8c\u8fdb\u5236\u4e32\u89e3\u91ca\u4e3aint\u6574\u6570\n\t\treturn getByteArray(intbits);\n\t}\n\t\/\/ \u4ecebyte\u6570\u7ec4\u7684index\u5904\u7684\u8fde\u7eed4\u4e2a\u5b57\u8282\u83b7\u5f97\u4e00\u4e2afloat\n\tpublic static float getFloat(byte[] arr, int index) {\n\t\treturn Float.intBitsToFloat(getInt(arr, index));\n\t}\n\t\/\/ long\u8f6c\u6362\u4e3abyte[8]\u6570\u7ec4\n\tpublic static byte[] getByteArray(long l) {\n\t\tbyte b[] = new byte[8];\n\t\tb[0] = (byte)  (0xff &amp; (l &gt;&gt; 56));\n\t\tb[1] = (byte)  (0xff &amp; (l &gt;&gt; 48));\n\t\tb[2] = (byte)  (0xff &amp; (l &gt;&gt; 40));\n\t\tb[3] = (byte)  (0xff &amp; (l &gt;&gt; 32));\n\t\tb[4] = (byte)  (0xff &amp; (l &gt;&gt; 24));\n\t\tb[5] = (byte)  (0xff &amp; (l &gt;&gt; 16));\n\t\tb[6] = (byte)  (0xff &amp; (l &gt;&gt; 8));\n\t\tb[7] = (byte)  (0xff &amp; l);\n\t\treturn b;\n\t}\n\t\/\/ \u4ecebyte\u6570\u7ec4\u7684index\u5904\u7684\u8fde\u7eed8\u4e2a\u5b57\u8282\u83b7\u5f97\u4e00\u4e2along\n\tpublic static long getLong(byte[] arr, int index) {\n\t\treturn \t(0xff00000000000000L \t&amp; ((long)arr[index+0] &lt;&lt; 56))  | \n\t\t\t\t(0x00ff000000000000L \t&amp; ((long)arr[index+1] &lt;&lt; 48))  | \n\t\t\t\t(0x0000ff0000000000L \t&amp; ((long)arr[index+2] &lt;&lt; 40))  | \n\t\t\t\t(0x000000ff00000000L \t&amp; ((long)arr[index+3] &lt;&lt; 32))  |\n\t\t\t\t(0x00000000ff000000L \t&amp; ((long)arr[index+4] &lt;&lt; 24))  | \n\t\t\t\t(0x0000000000ff0000L \t&amp; ((long)arr[index+5] &lt;&lt; 16))  | \n\t\t\t\t(0x000000000000ff00L \t&amp; ((long)arr[index+6] &lt;&lt; 8))   | \n\t\t\t\t(0x00000000000000ffL \t&amp;  (long)arr[index+7]);\n\t}\n\t\/\/ double\u8f6c\u6362\u4e3abyte[8]\u6570\u7ec4\n\tpublic static byte[] getByteArray(double d) {\n\t\tlong longbits = Double.doubleToLongBits(d);\n\t\treturn getByteArray(longbits);\n\t}\n\t\/\/ \u4ecebyte\u6570\u7ec4\u7684index\u5904\u7684\u8fde\u7eed8\u4e2a\u5b57\u8282\u83b7\u5f97\u4e00\u4e2adouble\n\tpublic static double getDouble(byte[] arr, int index) {\n\t\treturn Double.longBitsToDouble(getLong(arr, index));\n\t}\n \n\tpublic static void main(String[] args) {\n\t\tSystem.out.println(ByteOrder.nativeOrder());\n\t\tif(args.length &lt; 1){\n\t\t\tSystem.out.println(\"enter 'char' test method about char\");\n\t\t\tSystem.out.println(\"enter 'short' test method about short\");\n\t\t\tSystem.out.println(\"enter 'int' test method about int\");\n\t\t\tSystem.out.println(\"enter 'float' test method about float\");\n\t\t\tSystem.out.println(\"enter 'long' test method about long\");\n\t\t\tSystem.out.println(\"enter 'double' test method about double\");\n\t\t\treturn;\n\t\t}\n\t\tif(args[0].equals(\"char\")){\n\t\t\tchar c = 'u0000';\n\t\t\twhile( c &lt; 'uffff'){\n\t\t\t\tSystem.out.println(getChar(getByteArray(c),0));\n\t\t\t\tc++;\n\t\t\t}\n\t\t}else if(args[0].equals(\"short\")){\n\t\t\tshort s = Short.MIN_VALUE;\n\t\t\twhile( s &lt; Short.MAX_VALUE){\n\t\t\t\tSystem.out.println(getShort(getByteArray(s), 0));\n\t\t\t\ts++;\n\t\t\t}\n\t\t}else if(args[0].equals(\"int\")){\n\t\t\tint i = Integer.MIN_VALUE;\n\t\t\twhile( i &lt; Integer.MAX_VALUE){\n\t\t\t\tSystem.out.println(getInt(getByteArray(i), 0));\n\t\t\t\ti++;\n\t\t\t}\n\t\t}else if(args[0].equals(\"float\")){\n\t\t\tfloat f = Float.MIN_VALUE;\n\t\t\twhile(f &lt; Float.MAX_VALUE){\n\t\t\t\tSystem.out.println(getFloat(getByteArray(f), 0));\n\t\t\t\tf+=1.1111f;\n\t\t\t}\n\t\t}else if(args[0].equals(\"long\")){\n\t\t\tlong l = Long.MIN_VALUE;\n\t\t\twhile(l &lt; Long.MAX_VALUE){\n\t\t\t\tSystem.out.println(getLong(getByteArray(l), 0));\n\t\t\t\tl++;\n\t\t\t}\n\t\t}else if(args[0].equals(\"double\")){\n\t\t\tdouble d = Double.MIN_VALUE;\n\t\t\twhile(d &lt; Double.MAX_VALUE){\n\t\t\t\tSystem.out.println(getDouble(getByteArray(d), 0));\n\t\t\t\td+=1.111D;\n\t\t\t}\n\t\t}\n\t}\n}\n<\/pre>\n<\/div>\n<p>\u3000\u3000<\/p>\n","protected":false},"excerpt":{"rendered":"<p>package com.ioufev; import java.nio.ByteOrder; public c &hellip; <a href=\"http:\/\/blog.langmanezhuang.com\/index.php\/2021\/09\/16\/%e5%90%84%e7%a7%8d%e6%95%b0%e6%8d%ae%e7%b1%bb%e5%9e%8b%e8%bd%ac%e6%8d%a2%e4%b8%ba%e4%ba%8c%e8%bf%9b%e5%88%b6bytearrayconveter\/\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb <span class=\"screen-reader-text\">\u5404\u79cd\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u4e3a\u4e8c\u8fdb\u5236ByteArrayConveter<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"http:\/\/blog.langmanezhuang.com\/index.php\/wp-json\/wp\/v2\/posts\/852"}],"collection":[{"href":"http:\/\/blog.langmanezhuang.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.langmanezhuang.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.langmanezhuang.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.langmanezhuang.com\/index.php\/wp-json\/wp\/v2\/comments?post=852"}],"version-history":[{"count":0,"href":"http:\/\/blog.langmanezhuang.com\/index.php\/wp-json\/wp\/v2\/posts\/852\/revisions"}],"wp:attachment":[{"href":"http:\/\/blog.langmanezhuang.com\/index.php\/wp-json\/wp\/v2\/media?parent=852"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.langmanezhuang.com\/index.php\/wp-json\/wp\/v2\/categories?post=852"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.langmanezhuang.com\/index.php\/wp-json\/wp\/v2\/tags?post=852"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}