{"id":66827,"date":"2025-05-03T15:28:14","date_gmt":"2025-05-03T07:28:14","guid":{"rendered":"https:\/\/fwq.ai\/blog\/66827\/"},"modified":"2025-05-03T15:28:14","modified_gmt":"2025-05-03T07:28:14","slug":"java%e4%b8%adchar%e6%80%8e%e4%b9%88%e8%bd%ac%e5%ad%97%e8%8a%82%e6%95%b0%e7%bb%84-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/66827\/","title":{"rendered":"java\u4e2dchar\u600e\u4e48\u8f6c\u5b57\u8282\u6570\u7ec4"},"content":{"rendered":"<blockquote><p>\n  java\u4e2d\u5c06char\u8f6c\u6362\u4e3a\u5b57\u8282\u6570\u7ec4\u7684\u65b9\u6cd5\uff1a\u4f7f\u7528string.getbytes()\u65b9\u6cd5\u4f7f\u7528dataoutputstream\u7684writechar()\u65b9\u6cd5\u4f7f\u7528bytebuffer\u7684putchar()\u65b9\u6cd5\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/17\/2024111711272164210.jpg\" class=\"aligncenter\" title=\"java\u4e2dchar\u600e\u4e48\u8f6c\u5b57\u8282\u6570\u7ec4\u63d2\u56fe\" alt=\"java\u4e2dchar\u600e\u4e48\u8f6c\u5b57\u8282\u6570\u7ec4\u63d2\u56fe\" \/><\/p>\n<p><strong>Java \u4e2d Char \u8f6c\u6362\u4e3a\u5b57\u8282\u6570\u7ec4<\/strong><\/p>\n<p>\u8981\u5c06 char \u8f6c\u6362\u4e3a\u5b57\u8282\u6570\u7ec4\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\uff1a<\/p>\n<p><strong>1. \u4f7f\u7528 String.getBytes() \u65b9\u6cd5<\/strong><\/p>\n<p>\u6b64\u65b9\u6cd5\u5c06 String \u8f6c\u6362\u4e3a\u5b57\u8282\u6570\u7ec4\uff0c\u5e76\u4e14\u4e5f\u53ef\u4ee5\u7528\u4e8e char\uff0c\u56e0\u4e3a char \u53ef\u4ee5\u4e3a String\u3002<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>char c = 'a';\nbyte[] bytes = String.valueOf(c).getBytes();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \u4f7f\u7528 DataOutputStream<\/strong><\/p>\n<p>DataOutputStream \u53ef\u4ee5\u5c06\u57fa\u672c\u6570\u636e\u7c7b\u578b\u5199\u5165\u5b57\u8282\u6d41\u4e2d\u3002\u5b83\u4e5f\u53ef\u4ee5\u7528\u4e8e\u5c06 char \u8f6c\u6362\u4e3a\u5b57\u8282\u6570\u7ec4\u3002<\/p>\n<pre>ByteArrayOutputStream baos = new ByteArrayOutputStream();\nDataOutputStream dos = new DataOutputStream(baos);\ndos.writeChar(c);\nbyte[] bytes = baos.toByteArray();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3. \u4f7f\u7528 ByteBuffer<\/strong><\/p>\n<p>ByteBuffer \u662f\u4e00\u4e2a\u7528\u4e8e\u8bbf\u95ee\u548c\u4fee\u6539\u539f\u751f\u5b57\u8282\u6570\u636e\u7684\u7f13\u51b2\u533a\u3002\u5b83\u53ef\u4ee5\u7528\u6765\u521b\u5efa\u5b57\u8282\u6570\u7ec4\uff0c\u5176\u4e2d\u5305\u542b char \u7684\u5b57\u8282\u8868\u793a\u3002<\/p>\n<pre>ByteBuffer buffer = ByteBuffer.allocate(2);\nbuffer.putChar(c);\nbyte[] bytes = buffer.array();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u8f6c\u6362\u7ed3\u679c<\/strong><\/p>\n<p>Java \u4e2d char \u5360\u4e24\u4e2a\u5b57\u8282\uff0c\u56e0\u6b64\u8f6c\u6362\u4e3a\u5b57\u8282\u6570\u7ec4\u540e\uff0c\u7ed3\u679c\u5c06\u662f\u4e00\u4e2a\u957f\u5ea6\u4e3a 2 \u7684\u5b57\u8282\u6570\u7ec4\u3002\u5b57\u8282\u6570\u7ec4\u4e2d\u7684\u7b2c\u4e00\u4e2a\u5b57\u8282\u5305\u542b\u5b57\u7b26\u7684\u9ad8\u4f4d\u5b57\u8282\uff0c\u7b2c\u4e8c\u4e2a\u5b57\u8282\u5305\u542b\u4f4e\u4f4d\u5b57\u8282\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u4e2dchar\u600e\u4e48\u8f6c\u5b57\u8282\u6570\u7ec4\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8IDCBABY\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>java\u4e2d\u5c06char\u8f6c\u6362\u4e3a\u5b57\u8282\u6570\u7ec4\u7684\u65b9\u6cd5\uff1a\u4f7f\u7528string.getbytes()\u65b9\u6cd5\u4f7f\u7528dataoutputstream\u7684writechar()\u65b9\u6cd5\u4f7f\u7528bytebuffer\u7684putchar()\u65b9\u6cd5 Java \u4e2d Char \u8f6c\u6362\u4e3a\u5b57\u8282\u6570\u7ec4 \u8981\u5c06 char \u8f6c\u6362\u4e3a\u5b57\u8282\u6570\u7ec4\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\uff1a 1. \u4f7f\u7528 String.getBytes() \u65b9\u6cd5 \u6b64\u65b9\u6cd5\u5c06 String \u8f6c\u6362\u4e3a\u5b57\u8282\u6570\u7ec4\uff0c\u5e76\u4e14\u4e5f\u53ef\u4ee5\u7528\u4e8e char\uff0c\u56e0\u4e3a char \u53ef\u4ee5\u4e3a String\u3002 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b char c = &#8216;a&#8217;; byte[] bytes = String.valueOf(c).getBytes(); \u767b\u5f55\u540e\u590d\u5236 2. \u4f7f\u7528 DataOutputStream DataOutputStream \u53ef\u4ee5\u5c06\u57fa\u672c\u6570\u636e\u7c7b\u578b\u5199\u5165\u5b57\u8282\u6d41\u4e2d\u3002\u5b83\u4e5f\u53ef\u4ee5\u7528\u4e8e\u5c06 char \u8f6c\u6362\u4e3a\u5b57\u8282\u6570\u7ec4\u3002 ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(baos); dos.writeChar(c); byte[] bytes = baos.toByteArray(); \u767b\u5f55\u540e\u590d\u5236 3. \u4f7f\u7528 ByteBuffer [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-66827","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/66827","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=66827"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/66827\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=66827"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=66827"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=66827"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}