{"id":772,"date":"2023-03-13T09:31:51","date_gmt":"2023-03-13T01:31:51","guid":{"rendered":"http:\/\/blog.nonot.cn\/?p=772"},"modified":"2024-07-08T12:52:24","modified_gmt":"2024-07-08T04:52:24","slug":"java%e5%ad%97%e7%ac%a6%e4%b8%b2%e8%bd%ac%e7%a0%81","status":"publish","type":"post","link":"https:\/\/blog.nonot.cn\/index.php\/2023\/03\/13\/java%e5%ad%97%e7%ac%a6%e4%b8%b2%e8%bd%ac%e7%a0%81\/","title":{"rendered":"Java\u5b57\u7b26\u4e32\u8f6c\u7801"},"content":{"rendered":"<p>Java\u5728Windows\u4e2d\u6587\u7248\u7cfb\u7edf\u4e0a\u7684\u9ed8\u8ba4\u7f16\u7801\u662fGBK\uff0c\u800c\u5728Linux\u4e0a\u9ed8\u8ba4\u7f16\u7801\u662fUTF-8\uff0c\u4e24\u79cd\u7f16\u7801\u90fd\u652f\u6301\u4e2d\u6587\uff0c\u4f46\u662f\u5728\u4e24\u5e73\u53f0\u901a\u4fe1\u7684\u8fc7\u7a0b\u4e2d\u5982\u679c\u51fa\u73b0\u4e2d\u6587\u5f88\u53ef\u80fd\u5c31\u4f1a\u4e71\u7801\uff0c\u5f53\u7136\uff0c\u53ef\u4ee5\u628a\u9ed8\u8ba4\u7f16\u7801\u6539\u4e3aUTF-8\uff0c\u4f46\u662f\u8fd9\u662f\u6cbb\u6807\u4e0d\u6cbb\u672c\u7684\u505a\u6cd5\uff0c\u603b\u4f1a\u9047\u5230GBK\u7684<\/p>\n<p>\u8f6c\u7801\u539f\u7406\uff1a<br \/>\n\u7528getBytes()\u65b9\u6cd5\u4ee5UTF-8\u89e3\u7801\u539f\u672c\u7f16\u7801\u4e3aGBK\u7684\u5b57\u7b26\u4e32\uff0c\u4f7f\u5f97\u539f\u672c2byte\u4e00\u4f4d\u7684\u6c49\u5b57\u88ab\u8f6c\u6362\u4e3a3byte\u4e00\u4f4d\u7684UTF8\u7f16\u7801\u8868\u793a\u3002<br \/>\n\u7136\u540e\u7528String\u7684(byte[],charset)\u65b9\u6cd5\uff0c\u4ee5UTF8\u7f16\u7801\u91cd\u65b0\u7b80\u5386\u5b57\u7b26\u4e32\u3002<\/p>\n<p>\u4e00\u3001Java\u7684\u7f16\u7801<br \/>\nJava\u5728\u7f16\u8bd1\u8fc7\u7a0b\u4e2d\u4ee5Unicode\u5f62\u5f0f\u5b58\u50a8\u5b57\u7b26\uff1b<br \/>\n\u5728\u7a0b\u5e8f\u91cc\u521b\u5efa\u5b57\u7b26\u4e32\u7684\u65f6\u5019\uff0c\u5b57\u7b26\u4e32\u8fd8\u6ca1\u6709\u88ab\u7f16\u7801\uff0c\u76f4\u5230\u8f93\u5165\u8f93\u51fa\u65f6\uff0c\u5b57\u7b26\u4e32\u624d\u4f1a\u88ab\u4ee5\u7cfb\u7edf\u9ed8\u8ba4\u7f16\u7801\u5199\u5165 \u6216 \u4ee5\u7279\u5b9a\u7f16\u7801\u8f93\u51fa\u3002<\/p>\n<p>\u4e8c\u3001\u5b57\u7b26\u4e32\u8f6c\u6362\u7c7b<br \/>\n\u4ee3\u7801\u5982\u4e0b\uff1a<br \/>\n\u5411\u7c7b\u65b9\u6cd5\u4f20\u5165\u5f85\u8f6c\u7801\u7684\u5b57\u7b26\u4e32\uff0c\u8fd4\u56de\u503c\u4e3a<\/p>\n<p>import java.io.UnsupportedEncodingException;<\/p>\n<p>public class ArcoEncoding {<\/p>\n<p>public static void getBinary(String str) {<br \/>\nint length = 0;<br \/>\nString bstr;<br \/>\nfor(byte b:str.getBytes()){<br \/>\nbstr = Integer.toBinaryString((b &amp; 0xFF) + 0x100).substring(1);<br \/>\nSystem.out.print(bstr+&#8221; &#8220;);<br \/>\nlength++;<br \/>\n}<br \/>\nSystem.out.println(&#8220;|binary|&#8221;+length+&#8221; bit &#8220;+str);<br \/>\n}<\/p>\n<p>public static void getBinary(byte[] barray) {<br \/>\nint i;<br \/>\nString bstr;<br \/>\nfor(i = 0; i &lt;barray.length; i++){<br \/>\nbstr = Integer.toBinaryString((barray[i] &amp; 0xFF) + 0x100).substring(1);<br \/>\nSystem.out.print(bstr+&#8221; &#8220;);<br \/>\n}<br \/>\nSystem.out.println(&#8220;|bit array|length:&#8221;+barray.length+&#8221; bit&#8221;);<br \/>\n}<\/p>\n<p>public static byte[] toUTF8byte(String str) throws UnsupportedEncodingException {<\/p>\n<p>byte[] utf8byte;<br \/>\nutf8byte = str.getBytes(&#8220;UTF-8&#8221;);<\/p>\n<p>return utf8byte;<br \/>\n}<\/p>\n<p>public static String toUTF8str(String str) throws UnsupportedEncodingException {<br \/>\nbyte[] utf8byte;<br \/>\nString utf8str;<\/p>\n<p>utf8byte = str.getBytes(&#8220;UTF-8&#8243;);<\/p>\n<p>utf8str = new String(utf8byte,&#8221;UTF-8&#8221;);<\/p>\n<p>return utf8str;<br \/>\n}<br \/>\n}<\/p>\n<p>\u4e09\u3001\u6d4b\u8bd5<br \/>\n\u6d4b\u8bd5\u4ee3\u7801\u5982\u4e0b\uff1a<br \/>\n\u8fd0\u884c\u4ee3\u7801\uff0c\u5411\u63a7\u5236\u53f0\u8f93\u5165\u201c\u4e2d\u6587\u8f6c\u6362\u201d\uff08\u5176\u4ed6\u7684\u4e5f\u884c\uff0c\u6d4b\u8bd5\u56fe\u7247\u91cc\u9762\u8f93\u5165\u7684\u662f\u201c\u4e2d\u6587\u8f6c\u6362\u201d\uff09<br \/>\n\u7136\u540e\u4f1a\u8f93\u51fa\u8f6c\u7801\u7ed3\u679c\uff0c\u4ee5\u53ca\u8f6c\u6362\u8fc7\u7a0b\u4e2d\u7684\u4e8c\u8fdb\u5236\u7f16\u7801<\/p>\n<p>\/\/\u56e0\u4e3a\u8fd9\u91cc\u8c03\u7528\u4e86ArcoEncoding\u7c7b\u7684\u65b9\u6cd5\uff0c\u8981\u628a\u4e0a\u9762\u90a3\u4e2aArcoEncoding\u7c7b\u4e5f\u52a0\u5230\u540c\u4e00\u4e2a\u5305\u4e0b\u624d\u80fd\u8fd0\u884c<br \/>\nimport java.io.BufferedReader;<br \/>\nimport java.io.InputStreamReader;<\/p>\n<p>public class Test {<br \/>\npublic static void main(String args[]) throws Exception{<br \/>\nString str = &#8220;&#8221;;<br \/>\nbyte[] codedarray;<br \/>\nString codedstr = &#8220;&#8221;;<\/p>\n<p>BufferedReader br = new BufferedReader(new InputStreamReader(System.in));<br \/>\nstr = br.readLine();<\/p>\n<p>System.out.print(&#8220;\u539f\u7f16\u7801(\u4e8c\u8fdb\u5236):&#8221;);<br \/>\nArcoEncoding.getBinary(str);\/\/binary out<\/p>\n<p>codedarray = ArcoEncoding.toUTF8byte(str);<\/p>\n<p>System.out.print(&#8220;\u8f6c\u7801\u7ed3\u679c(\u4e8c\u8fdb\u5236):&#8221;);<br \/>\nArcoEncoding.getBinary(codedarray);\/\/binary out<\/p>\n<p>codedstr = ArcoEncoding.toUTF8str(str);<\/p>\n<p>System.out.print(&#8220;\u8f6c\u7801\u7ed3\u679c(\u5b57\u7b26\u4e32):&#8221;+codedstr);<\/p>\n<p>}<br \/>\n}<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Java\u5728Windows\u4e2d\u6587\u7248\u7cfb\u7edf\u4e0a\u7684\u9ed8\u8ba4\u7f16\u7801\u662fGBK\uff0c\u800c\u5728&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-772","post","type-post","status-publish","format-standard","hentry","category-java"],"_links":{"self":[{"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/posts\/772","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/comments?post=772"}],"version-history":[{"count":1,"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/posts\/772\/revisions"}],"predecessor-version":[{"id":1209,"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/posts\/772\/revisions\/1209"}],"wp:attachment":[{"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/media?parent=772"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/categories?post=772"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/tags?post=772"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}