- 走看看
zoukankan      html  css  js  c++  java
  • </h1> <div style="border: 1px solid #dfdfdf;border-top:none;"></div> <div style=" height: 200px; overflow:hidden;"> <span class="pre-scrollable"><div id="cnblogs_post_body" class="blogpost-body "> <div><br></div><div><div><div><h1 style="margin-top: 8px; margin-bottom: 8px; padding: 5px 15px; max- 100%; border: 0px currentcolor; border-image-source: initial; border-image-slice: initial; border-image- initial; border-image-outset: initial; border-image-repeat: initial; border-radius: 5px 5px 0px 0px; box-sizing: border-box !important; word-wrap: break-word !important; background-color: rgb(255, 81, 0);"><font color="#ffffff"><span style="font-size: 18.6667px; line-height: 25px;">Unicode的编码形式与对应的字符串相互转换</span></font></h2></div></div></div><!--more--><div style="font-size: 14.6667px; line-height: 17.6px;"><pre style="margin-top: 0px; margin-bottom: 0px; font-size: 14.6667px; line-height: 22.0001px; word-wrap: break-word;"><div style="font-family: 微软雅黑, Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; font-size: 14.6667px; line-height: 22px; white-space: normal;"><span style="font-size: 11pt; color: rgb(63, 95, 191); font-family: 微软雅黑;">/**</span></div><div style="font-family: 微软雅黑, Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; font-size: 14.6667px; line-height: 22px; white-space: normal;"><span style="font-size: 11pt; color: rgb(63, 95, 191); font-family: 微软雅黑;"> * Unicode的编码形式与对应的字符串相互转换</span></div><div style="font-family: 微软雅黑, Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; font-size: 14.6667px; line-height: 22px; white-space: normal;"><span style="font-size: 11pt; color: rgb(63, 95, 191); font-family: 微软雅黑;"> * <span style="font-weight: bold; color: rgb(127, 159, 191);">@author</span> 白乾涛</span></div><div style="font-family: 微软雅黑, Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; font-size: 14.6667px; line-height: 22px; white-space: normal;"><span style="font-size: 11pt; color: rgb(63, 95, 191); font-family: 微软雅黑;"> */</span></div><div style="font-family: 微软雅黑, Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; font-size: 14.6667px; line-height: 22px; white-space: normal;"><span style="font-weight: bold; font-size: 11pt; color: rgb(127, 0, 85); font-family: 微软雅黑;">public<span style="font-weight: normal; color: windowtext;"> </span>class<span style="font-weight: normal; color: windowtext;"> UnicodeUtils <span style="color: rgb(128, 0, 128);">{</span></span></span></div><div style="font-family: 微软雅黑, Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; font-size: 14.6667px; line-height: 22px; white-space: normal;"><span style="font-size: 11pt; color: windowtext; font-family: 微软雅黑;">    <span style="font-weight: bold; color: rgb(127, 0, 85);">public</span> <span style="font-weight: bold; color: rgb(127, 0, 85);">static</span> <span style="font-weight: bold; color: rgb(127, 0, 85);">void</span> main<span style="color: rgb(128, 0, 128);">(</span>String<span style="color: rgb(128, 0, 128);">[]</span> args<span style="color: rgb(128, 0, 128);">)</span> <span style="font-weight: bold; color: rgb(127, 0, 85);">throws</span> UnsupportedEncodingException <span style="color: rgb(128, 0, 128);">{</span></span></div><div style="font-family: 微软雅黑, Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; font-size: 14.6667px; line-height: 22px; white-space: normal;"><span style="font-size: 11pt; color: windowtext; font-family: 微软雅黑;">        <span style="font-style: italic;">test1<span style="font-style: normal; color: rgb(128, 0, 128);">()</span></span>;</span></div><div style="font-family: 微软雅黑, Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; font-size: 14.6667px; line-height: 22px; white-space: normal;"><span style="font-size: 11pt; color: windowtext; font-family: 微软雅黑;">        <span style="font-style: italic;">test2<span style="font-style: normal; color: rgb(128, 0, 128);">()</span></span>;</span></div><div style="font-family: 微软雅黑, Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; font-size: 14.6667px; line-height: 22px; white-space: normal;"><span style="font-size: 11pt; color: windowtext; font-family: 微软雅黑;">        <span style="color: rgb(0, 128, 0);">//服务器返回的很可能是这种形式的字符串</span></span></div><div style="font-family: 微软雅黑, Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; font-size: 14.6667px; line-height: 22px; white-space: normal;"><span style="font-size: 11pt; color: windowtext; font-family: 微软雅黑;">        String unicodeMessages = <span style="color: rgb(42, 0, 255);">"{"code":200,"message":"\u4fdd\u5b58\u6210\u529f"}"</span>;<span style="color: rgb(0, 128, 0);">//所有的引号和反斜杠前面都要加一个反斜杠</span></span></div><div style="font-family: 微软雅黑, Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; font-size: 14.6667px; line-height: 22px; white-space: normal;"><span style="font-size: 11pt; color: windowtext; font-family: 微软雅黑;">        System.<span style="font-style: italic; color: rgb(0, 0, 192);">out</span>.println<span style="color: rgb(128, 0, 128);">(<span style="font-style: italic; color: windowtext;">unicodeToString</span>(</span>unicodeMessages<span style="color: rgb(128, 0, 128);">)</span> + <span style="color: rgb(42, 0, 255);">" "<span style="color: rgb(128, 0, 128);">)</span></span>;<span style="color: rgb(0, 128, 0);">//{"code":200,"message":"保存成功"}</span></span></div><div style="font-family: 微软雅黑, Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; font-size: 14.6667px; line-height: 22px; white-space: normal;"><span style="font-size: 11pt; color: windowtext; font-family: 微软雅黑;">    <span style="color: rgb(128, 0, 128);">}</span></span></div><div style="font-family: 微软雅黑, Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; font-size: 14.6667px; line-height: 22px; white-space: normal;"></div><div style="font-family: 微软雅黑, Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; font-size: 14.6667px; line-height: 22px; white-space: normal;"><span style="font-size: 11pt; color: windowtext; font-family: 微软雅黑;">    <span style="font-weight: bold; color: rgb(127, 0, 85);">private</span> <span style="font-weight: bold; color: rgb(127, 0, 85);">static</span> <span style="font-weight: bold; color: rgb(127, 0, 85);">void</span> test1<span style="color: rgb(128, 0, 128);">()</span> <span style="color: rgb(128, 0, 128);">{</span></span></div><div style="font-family: 微软雅黑, Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; font-size: 14.6667px; line-height: 22px; white-space: normal;"><span style="font-size: 11pt; color: windowtext; font-family: 微软雅黑;">        System.<span style="font-style: italic; color: rgb(0, 0, 192);">out</span>.println<span style="color: rgb(128, 0, 128);">(<span style="color: rgb(42, 0, 255);">"【汉】的Unicode编码为【"</span></span> + <span style="font-style: italic;">toUnicodeString<span style="font-style: normal; color: rgb(128, 0, 128);">(<span style="color: rgb(42, 0, 255);">'汉'</span>)</span></span> + <span style="color: rgb(42, 0, 255);">"】"<span style="color: rgb(128, 0, 128);">)</span></span>;<span style="color: rgb(0, 128, 0);">//【汉】的Unicode编码为【u6c49】</span></span></div><div style="font-family: 微软雅黑, Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; font-size: 14.6667px; line-height: 22px; white-space: normal;"><span style="font-size: 11pt; color: windowtext; font-family: 微软雅黑;">        System.<span style="font-style: italic; color: rgb(0, 0, 192);">out</span>.println<span style="color: rgb(128, 0, 128);">(<span style="color: rgb(42, 0, 255);">"【\u6c49】对应的字符为【"</span></span> + <span style="font-style: italic;">unicodeToString<span style="font-style: normal; color: rgb(128, 0, 128);">(<span style="color: rgb(42, 0, 255);">"u6c49"</span>)</span></span> + <span style="color: rgb(42, 0, 255);">"】"<span style="color: rgb(128, 0, 128);">)</span></span>;<span style="color: rgb(0, 128, 0);">//【u6c49】对应的字符为【汉】</span></span></div><div style="font-family: 微软雅黑, Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; font-size: 14.6667px; line-height: 22px; white-space: normal;"><span style="font-size: 11pt; color: windowtext; font-family: 微软雅黑;">        System.<span style="font-style: italic; color: rgb(0, 0, 192);">out</span>.println<span style="color: rgb(128, 0, 128);">(<span style="color: rgb(42, 0, 255);">"u6c49"</span>)</span>;<span style="color: rgb(0, 128, 0);">//会自动解码【汉】</span></span></div><div style="font-family: 微软雅黑, Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; font-size: 14.6667px; line-height: 22px; white-space: normal;"><span style="font-size: 11pt; color: windowtext; font-family: 微软雅黑;">        System.<span style="font-style: italic; color: rgb(0, 0, 192);">out</span>.println<span style="color: rgb(128, 0, 128);">(</span>0x9FFF - 0x3000 + <span style="color: rgb(42, 0, 255);">" "<span style="color: rgb(128, 0, 128);">)</span></span>;<span style="color: rgb(0, 128, 0);">//【28671】</span></span></div><div style="font-family: 微软雅黑, Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; font-size: 14.6667px; line-height: 22px; white-space: normal;"><span style="font-size: 11pt; color: windowtext; font-family: 微软雅黑;">    <span style="color: rgb(128, 0, 128);">}</span></span></div><div style="font-family: 微软雅黑, Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; font-size: 14.6667px; line-height: 22px; white-space: normal;"></div><div style="font-family: 微软雅黑, Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; font-size: 14.6667px; line-height: 22px; white-space: normal;"><span style="font-size: 11pt; color: windowtext; font-family: 微软雅黑;">    <span style="font-weight: bold; color: rgb(127, 0, 85);">private</span> <span style="font-weight: bold; color: rgb(127, 0, 85);">static</span> <span style="font-weight: bold; color: rgb(127, 0, 85);">void</span> test2<span style="color: rgb(128, 0, 128);">()</span> <span style="color: rgb(128, 0, 128);">{</span></span></div><div style="font-family: 微软雅黑, Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; font-size: 14.6667px; line-height: 22px; white-space: normal;"><span style="font-size: 11pt; color: windowtext; font-family: 微软雅黑;">        String string = <span style="color: rgb(42, 0, 255);">"aA1:中国"</span>;</span></div><div style="font-family: 微软雅黑, Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; font-size: 14.6667px; line-height: 22px; white-space: normal;"><span style="font-size: 11pt; color: windowtext; font-family: 微软雅黑;">        String unicode = <span style="font-style: italic;">toUnicodeString<span style="font-style: normal; color: rgb(128, 0, 128);">(</span></span>string<span style="color: rgb(128, 0, 128);">)</span>;</span></div><div style="font-family: 微软雅黑, Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; font-size: 14.6667px; line-height: 22px; white-space: normal;"><span style="font-size: 11pt; color: windowtext; font-family: 微软雅黑;">        System.<span style="font-style: italic; color: rgb(0, 0, 192);">out</span>.println<span style="color: rgb(128, 0, 128);">(<span style="color: rgb(42, 0, 255);">"【"</span></span> + string + <span style="color: rgb(42, 0, 255);">"】的Unicode编码为【"</span> + unicode + <span style="color: rgb(42, 0, 255);">"】"<span style="color: rgb(128, 0, 128);">)</span></span>;<span style="color: rgb(0, 128, 0);">//【u0061u0041u0031u003au4e2du56fd】</span></span></div><div style="font-family: 微软雅黑, Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; font-size: 14.6667px; line-height: 22px; white-space: normal;"><span style="font-size: 11pt; color: windowtext; font-family: 微软雅黑;">        System.<span style="font-style: italic; color: rgb(0, 0, 192);">out</span>.println<span style="color: rgb(128, 0, 128);">(<span style="font-style: italic; color: windowtext;">unicodeToString</span>(</span>unicode<span style="color: rgb(128, 0, 128);">))</span>;<span style="color: rgb(0, 128, 0);">//【aA1:中国】</span></span></div><div style="font-family: 微软雅黑, Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; font-size: 14.6667px; line-height: 22px; white-space: normal;"><span style="font-size: 11pt; color: windowtext; font-family: 微软雅黑;">        <span style="font-weight: bold; color: rgb(127, 0, 85);">for</span> <span style="color: rgb(128, 0, 128);">(<span style="font-weight: bold; color: rgb(127, 0, 85);">int</span></span> i = 0; i < unicode.length<span style="color: rgb(128, 0, 128);">()</span>; i += 6<span style="color: rgb(128, 0, 128);">)</span> <span style="color: rgb(128, 0, 128);">{</span></span></div><div style="font-family: 微软雅黑, Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; font-size: 14.6667px; line-height: 22px; white-space: normal;"><span style="font-size: 11pt; color: windowtext; font-family: 微软雅黑;">            System.<span style="font-style: italic; color: rgb(0, 0, 192);">out</span>.print<span style="color: rgb(128, 0, 128);">(</span>unicode.substring<span style="color: rgb(128, 0, 128);">(</span>i, i + 6<span style="color: rgb(128, 0, 128);">))</span>;<span style="color: rgb(0, 128, 0);">//并不会解码,只是把Unicode编码形式的字符串打印出来,为什么呢?</span></span></div><div style="font-family: 微软雅黑, Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; font-size: 14.6667px; line-height: 22px; white-space: normal;"><span style="font-size: 11pt; color: windowtext; font-family: 微软雅黑;">        <span style="color: rgb(128, 0, 128);">}</span></span></div><div style="font-family: 微软雅黑, Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; font-size: 14.6667px; line-height: 22px; white-space: normal;"><span style="font-size: 11pt; color: windowtext; font-family: 微软雅黑;">        System.<span style="font-style: italic; color: rgb(0, 0, 192);">out</span>.println<span style="color: rgb(128, 0, 128);">(<span style="color: rgb(42, 0, 255);">" "</span></span> + unicode<span style="color: rgb(128, 0, 128);">)</span>;<span style="color: rgb(0, 128, 0);">//同样,这里也不会解码。【u0061u0041u0031u003au4e2du56fd】</span></span></div><div style="font-family: 微软雅黑, Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; font-size: 14.6667px; line-height: 22px; white-space: normal;"><span style="font-size: 11pt; color: windowtext; font-family: 微软雅黑;">    <span style="color: rgb(128, 0, 128);">}</span></span></div><div style="font-family: 微软雅黑, Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; font-size: 14.6667px; line-height: 22px; white-space: normal;"></div><div style="font-family: 微软雅黑, Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; font-size: 14.6667px; line-height: 22px; white-space: normal;"><span style="font-size: 11pt; color: windowtext; font-family: 微软雅黑;">    <span style="color: rgb(63, 95, 191);">/**</span></span></div><div style="font-family: 微软雅黑, Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; font-size: 14.6667px; line-height: 22px; white-space: normal;"><span style="font-size: 11pt; color: rgb(63, 95, 191); font-family: 微软雅黑;">     * 将【字符】转换成Unicode码形式【Unicode用两个字节来编码一个字符,2^16=16^4,所以可以用四个16进制数表示】</span></div><div style="font-family: 微软雅黑, Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; font-size: 14.6667px; line-height: 22px; white-space: normal;"><span style="font-size: 11pt; color: rgb(63, 95, 191); font-family: 微软雅黑;">     */</span></div><div style="font-family: 微软雅黑, Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; font-size: 14.6667px; line-height: 22px; white-space: normal;"><span style="font-size: 11pt; color: windowtext; font-family: 微软雅黑;">    <span style="font-weight: bold; color: rgb(127, 0, 85);"><span data-wiz-span="data-wiz-span" style="background-color: rgb(0, 204, 255);">public</span></span><span data-wiz-span="data-wiz-span" style="background-color: rgb(0, 204, 255);"> </span><span style="font-weight: bold; color: rgb(127, 0, 85);"><span data-wiz-span="data-wiz-span" style="background-color: rgb(0, 204, 255);">static</span></span><span data-wiz-span="data-wiz-span" style="background-color: rgb(0, 204, 255);"> String toUnicodeString</span><span style="color: rgb(128, 0, 128);"><span data-wiz-span="data-wiz-span" style="background-color: rgb(0, 204, 255);">(</span><span style="font-weight: bold; color: rgb(127, 0, 85);"><span data-wiz-span="data-wiz-span" style="background-color: rgb(0, 204, 255);">char</span></span></span><span data-wiz-span="data-wiz-span" style="background-color: rgb(0, 204, 255);"> c</span><span style="color: rgb(128, 0, 128);"><span data-wiz-span="data-wiz-span" style="background-color: rgb(0, 204, 255);">)</span></span><span data-wiz-span="data-wiz-span" style="background-color: rgb(0, 204, 255);"> </span><span style="color: rgb(128, 0, 128);"><span data-wiz-span="data-wiz-span" style="background-color: rgb(0, 204, 255);">{</span></span></span></div><div style="font-family: 微软雅黑, Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; font-size: 14.6667px; line-height: 22px; white-space: normal;"><span style="font-size: 11pt; color: windowtext; font-family: 微软雅黑;">        StringBuilder sb = <span style="font-weight: bold; color: rgb(127, 0, 85);">new</span> StringBuilder<span style="color: rgb(128, 0, 128);">(<span style="color: rgb(42, 0, 255);">""</span>)</span>;</span></div><div style="font-family: 微软雅黑, Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; font-size: 14.6667px; line-height: 22px; white-space: normal;"><span style="font-size: 11pt; color: windowtext; font-family: 微软雅黑;">        <span data-wiz-span="data-wiz-span" style="background-color: rgb(0, 204, 255);">String hexString = Integer.</span><span style="font-style: italic;"><span data-wiz-span="data-wiz-span" style="background-color: rgb(0, 204, 255);">toHexString</span><span style="font-style: normal; color: rgb(128, 0, 128);"><span data-wiz-span="data-wiz-span" style="background-color: rgb(0, 204, 255);">(</span></span></span><span data-wiz-span="data-wiz-span" style="background-color: rgb(0, 204, 255);">c</span><span style="color: rgb(128, 0, 128);"><span data-wiz-span="data-wiz-span" style="background-color: rgb(0, 204, 255);">)</span></span><span data-wiz-span="data-wiz-span" style="background-color: rgb(0, 204, 255);">;</span></span></div><div style="font-family: 微软雅黑, Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; font-size: 14.6667px; line-height: 22px; white-space: normal;"><span style="font-size: 11pt; color: windowtext; font-family: 微软雅黑;">        <span data-wiz-span="data-wiz-span" style="background-color: rgb(0, 204, 255);">sb.append</span><span style="color: rgb(128, 0, 128);"><span data-wiz-span="data-wiz-span" style="background-color: rgb(0, 204, 255);">(</span><span style="color: rgb(42, 0, 255);"><span data-wiz-span="data-wiz-span" style="background-color: rgb(0, 204, 255);">"\u"</span></span><span data-wiz-span="data-wiz-span" style="background-color: rgb(0, 204, 255);">)</span></span><span data-wiz-span="data-wiz-span" style="background-color: rgb(0, 204, 255);">;</span><span style="color: rgb(0, 128, 0);">//用于标识这是一个Unicode码</span></span></div><div style="font-family: 微软雅黑, Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; font-size: 14.6667px; line-height: 22px; white-space: normal;"><span style="font-size: 11pt; color: windowtext; font-family: 微软雅黑;">        <span style="font-weight: bold; color: rgb(127, 0, 85);">for</span> <span style="color: rgb(128, 0, 128);">(<span style="font-weight: bold; color: rgb(127, 0, 85);">int</span></span> j = hexString.length<span style="color: rgb(128, 0, 128);">()</span>; j < 4; j++<span style="color: rgb(128, 0, 128);">)</span> <span style="color: rgb(128, 0, 128);">{</span></span></div><div style="font-family: 微软雅黑, Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; font-size: 14.6667px; line-height: 22px; white-space: normal;"><span style="font-size: 11pt; color: windowtext; font-family: 微软雅黑;">           <span data-wiz-span="data-wiz-span" style="background-color: rgb(0, 204, 255);"> sb.append</span><span style="color: rgb(128, 0, 128);"><span data-wiz-span="data-wiz-span" style="background-color: rgb(0, 204, 255);">(</span></span><span data-wiz-span="data-wiz-span" style="background-color: rgb(0, 204, 255);">0</span><span style="color: rgb(128, 0, 128);"><span data-wiz-span="data-wiz-span" style="background-color: rgb(0, 204, 255);">)</span></span><span data-wiz-span="data-wiz-span" style="background-color: rgb(0, 204, 255);">;</span><span style="color: rgb(0, 128, 0);">//不足四位用0填充</span></span></div><div style="font-family: 微软雅黑, Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; font-size: 14.6667px; line-height: 22px; white-space: normal;"><span style="font-size: 11pt; color: windowtext; font-family: 微软雅黑;">        <span style="color: rgb(128, 0, 128);">}</span></span></div><div style="font-family: 微软雅黑, Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; font-size: 14.6667px; line-height: 22px; white-space: normal;"><span style="font-size: 11pt; color: windowtext; font-family: 微软雅黑;">        sb.append<span style="color: rgb(128, 0, 128);">(</span>hexString<span style="color: rgb(128, 0, 128);">)</span>;</span></div><div style="font-family: 微软雅黑, Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; font-size: 14.6667px; line-height: 22px; white-space: normal;"><span style="font-size: 11pt; color: windowtext; font-family: 微软雅黑;">        <span style="font-weight: bold; color: rgb(127, 0, 85);">return</span> sb.toString<span style="color: rgb(128, 0, 128);">()</span>;</span></div><div style="font-family: 微软雅黑, Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; font-size: 14.6667px; line-height: 22px; white-space: normal;"><span style="font-size: 11pt; color: windowtext; font-family: 微软雅黑;">    <span style="color: rgb(128, 0, 128);">}</span></span></div><div style="font-family: 微软雅黑, Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; font-size: 14.6667px; line-height: 22px; white-space: normal;"></div><div style="font-family: 微软雅黑, Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; font-size: 14.6667px; line-height: 22px; white-space: normal;"><span style="font-size: 11pt; color: windowtext; font-family: 微软雅黑;">    <span style="color: rgb(63, 95, 191);">/**</span></span></div><div style="font-family: 微软雅黑, Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; font-size: 14.6667px; line-height: 22px; white-space: normal;"><span style="font-size: 11pt; color: rgb(63, 95, 191); font-family: 微软雅黑;">     * 将【字符串】转换成Unicode码形式</span></div><div style="font-family: 微软雅黑, Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; font-size: 14.6667px; line-height: 22px; white-space: normal;"><span style="font-size: 11pt; color: rgb(63, 95, 191); font-family: 微软雅黑;">     */</span></div><div style="font-family: 微软雅黑, Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; font-size: 14.6667px; line-height: 22px; white-space: normal;"><span style="font-size: 11pt; color: windowtext; font-family: 微软雅黑;">    <span style="font-weight: bold; color: rgb(127, 0, 85);"><span data-wiz-span="data-wiz-span" style="background-color: rgb(0, 204, 255);">public</span></span><span data-wiz-span="data-wiz-span" style="background-color: rgb(0, 204, 255);"> </span><span style="font-weight: bold; color: rgb(127, 0, 85);"><span data-wiz-span="data-wiz-span" style="background-color: rgb(0, 204, 255);">static</span></span><span data-wiz-span="data-wiz-span" style="background-color: rgb(0, 204, 255);"> String toUnicodeString</span><span style="color: rgb(128, 0, 128);"><span data-wiz-span="data-wiz-span" style="background-color: rgb(0, 204, 255);">(</span></span><span data-wiz-span="data-wiz-span" style="background-color: rgb(0, 204, 255);">String string</span><span style="color: rgb(128, 0, 128);"><span data-wiz-span="data-wiz-span" style="background-color: rgb(0, 204, 255);">)</span></span><span data-wiz-span="data-wiz-span" style="background-color: rgb(0, 204, 255);"> </span><span style="color: rgb(128, 0, 128);"><span data-wiz-span="data-wiz-span" style="background-color: rgb(0, 204, 255);">{</span></span></span></div><div style="font-family: 微软雅黑, Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; font-size: 14.6667px; line-height: 22px; white-space: normal;"><span style="font-size: 11pt; color: windowtext; font-family: 微软雅黑;">        StringBuilder sb = <span style="font-weight: bold; color: rgb(127, 0, 85);">new</span> StringBuilder<span style="color: rgb(128, 0, 128);">(<span style="color: rgb(42, 0, 255);">""</span>)</span>;</span></div><div style="font-family: 微软雅黑, Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; font-size: 14.6667px; line-height: 22px; white-space: normal;"><span style="font-size: 11pt; color: windowtext; font-family: 微软雅黑;">        <span style="font-weight: bold; color: rgb(127, 0, 85);">for</span> <span style="color: rgb(128, 0, 128);">(<span style="font-weight: bold; color: rgb(127, 0, 85);">int</span></span> i = 0; i < string.length<span style="color: rgb(128, 0, 128);">()</span>; i++<span style="color: rgb(128, 0, 128);">)</span> <span style="color: rgb(128, 0, 128);">{</span></span></div><div style="font-family: 微软雅黑, Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; font-size: 14.6667px; line-height: 22px; white-space: normal;"><span style="font-size: 11pt; color: windowtext; font-family: 微软雅黑;">            sb.append<span style="color: rgb(128, 0, 128);">(<span style="font-style: italic; color: windowtext;">toUnicodeString</span>(</span>string.charAt<span style="color: rgb(128, 0, 128);">(</span>i<span style="color: rgb(128, 0, 128);">)))</span>;</span></div><div style="font-family: 微软雅黑, Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; font-size: 14.6667px; line-height: 22px; white-space: normal;"><span style="font-size: 11pt; color: windowtext; font-family: 微软雅黑;">        <span style="color: rgb(128, 0, 128);">}</span></span></div><div style="font-family: 微软雅黑, Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; font-size: 14.6667px; line-height: 22px; white-space: normal;"><span style="font-size: 11pt; color: windowtext; font-family: 微软雅黑;">        <span style="font-weight: bold; color: rgb(127, 0, 85);">return</span> sb.toString<span style="color: rgb(128, 0, 128);">()</span>;</span></div><div style="font-family: 微软雅黑, Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; font-size: 14.6667px; line-height: 22px; white-space: normal;"><span style="font-size: 11pt; color: windowtext; font-family: 微软雅黑;">    <span style="color: rgb(128, 0, 128);">}</span></span></div><div style="font-family: 微软雅黑, Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; font-size: 14.6667px; line-height: 22px; white-space: normal;"></div><div style="font-family: 微软雅黑, Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; font-size: 14.6667px; line-height: 22px; white-space: normal;"><span style="font-size: 11pt; color: windowtext; font-family: 微软雅黑;">    <span style="color: rgb(63, 95, 191);">/**</span></span></div><div style="font-family: 微软雅黑, Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; font-size: 14.6667px; line-height: 22px; white-space: normal;"><span style="font-size: 11pt; color: rgb(63, 95, 191); font-family: 微软雅黑;">     * 将Unicode编码解析成字符串形式</span></div><div style="font-family: 微软雅黑, Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; font-size: 14.6667px; line-height: 22px; white-space: normal;"><span style="font-size: 11pt; color: rgb(63, 95, 191); font-family: 微软雅黑;">     */</span></div><div style="font-family: 微软雅黑, Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; font-size: 14.6667px; line-height: 22px; white-space: normal;"><span style="font-size: 11pt; color: windowtext; font-family: 微软雅黑;">    <span style="font-weight: bold; color: rgb(127, 0, 85);">public</span> <span style="font-weight: bold; color: rgb(127, 0, 85);">static</span> String unicodeToString<span style="color: rgb(128, 0, 128);">(</span>String uString<span style="color: rgb(128, 0, 128);">)</span> <span style="color: rgb(128, 0, 128);">{</span></span></div><div style="font-family: 微软雅黑, Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; font-size: 14.6667px; line-height: 22px; white-space: normal;"><span style="font-size: 11pt; color: windowtext; font-family: 微软雅黑;">        StringBuilder sb = <span style="font-weight: bold; color: rgb(127, 0, 85);">new</span> StringBuilder<span style="color: rgb(128, 0, 128);">()</span>;</span></div><div style="font-family: 微软雅黑, Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; font-size: 14.6667px; line-height: 22px; white-space: normal;"><span style="font-size: 11pt; color: windowtext; font-family: 微软雅黑;">        <span style="font-weight: bold; color: rgb(127, 0, 85);">int</span> i = -1, pos = 0;</span></div><div style="font-family: 微软雅黑, Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; font-size: 14.6667px; line-height: 22px; white-space: normal;"><span style="font-size: 11pt; color: windowtext; font-family: 微软雅黑;">        <span style="font-weight: bold; color: rgb(127, 0, 85);">while</span> <span style="color: rgb(128, 0, 128);">((</span>i = uString.indexOf<span style="color: rgb(128, 0, 128);">(<span style="color: rgb(42, 0, 255);">"\u"</span></span>, pos<span style="color: rgb(128, 0, 128);">))</span> != -1<span style="color: rgb(128, 0, 128);">)</span> <span style="color: rgb(128, 0, 128);">{</span></span></div><div style="font-family: 微软雅黑, Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; font-size: 14.6667px; line-height: 22px; white-space: normal;"><span style="font-size: 11pt; color: windowtext; font-family: 微软雅黑;">            sb.append<span style="color: rgb(128, 0, 128);">(</span>uString.substring<span style="color: rgb(128, 0, 128);">(</span>pos, i<span style="color: rgb(128, 0, 128);">))</span>;</span></div><div style="font-family: 微软雅黑, Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; font-size: 14.6667px; line-height: 22px; white-space: normal;"><span style="font-size: 11pt; color: windowtext; font-family: 微软雅黑;">            <span style="font-weight: bold; color: rgb(127, 0, 85);">if</span> <span style="color: rgb(128, 0, 128);">(</span>i + 5 < uString.length<span style="color: rgb(128, 0, 128);">())</span> <span style="color: rgb(128, 0, 128);">{</span></span></div><div style="font-family: 微软雅黑, Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; font-size: 14.6667px; line-height: 22px; white-space: normal;"><span style="font-size: 11pt; color: windowtext; font-family: 微软雅黑;">                pos = i + 6;</span></div><div style="font-family: 微软雅黑, Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; font-size: 14.6667px; line-height: 22px; white-space: normal;"><span style="font-size: 11pt; color: windowtext; font-family: 微软雅黑;">                sb.append<span style="color: rgb(128, 0, 128);">((<span style="font-weight: bold; color: rgb(127, 0, 85);">char</span>)</span> Integer.<span style="font-style: italic;">parseInt<span style="font-style: normal; color: rgb(128, 0, 128);">(</span></span>uString.substring<span style="color: rgb(128, 0, 128);">(</span>i + 2, i + 6<span style="color: rgb(128, 0, 128);">)</span>, 16<span style="color: rgb(128, 0, 128);">))</span>;</span></div><div style="font-family: 微软雅黑, Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; font-size: 14.6667px; line-height: 22px; white-space: normal;"><span style="font-size: 11pt; color: windowtext; font-family: 微软雅黑;">            <span style="color: rgb(128, 0, 128);">}</span></span></div><div style="font-family: 微软雅黑, Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; font-size: 14.6667px; line-height: 22px; white-space: normal;"><span style="font-size: 11pt; color: windowtext; font-family: 微软雅黑;">        <span style="color: rgb(128, 0, 128);">}</span></span></div><div style="font-family: 微软雅黑, Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; font-size: 14.6667px; line-height: 22px; white-space: normal;"><span style="font-size: 11pt; color: windowtext; font-family: 微软雅黑;">        sb.append<span style="color: rgb(128, 0, 128);">(</span>uString.substring<span style="color: rgb(128, 0, 128);">(</span>pos<span style="color: rgb(128, 0, 128);">))</span>;</span></div><div style="font-family: 微软雅黑, Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; font-size: 14.6667px; line-height: 22px; white-space: normal;"><span style="font-size: 11pt; color: windowtext; font-family: 微软雅黑;">        <span style="font-weight: bold; color: rgb(127, 0, 85);">return</span> sb.toString<span style="color: rgb(128, 0, 128);">()</span>;</span></div><div style="font-family: 微软雅黑, Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; font-size: 14.6667px; line-height: 22px; white-space: normal;"><span style="font-size: 11pt; color: windowtext; font-family: 微软雅黑;">    <span style="color: rgb(128, 0, 128);">}</span></span></div><span style="font-size: 11pt; line-height: 22px; white-space: normal; color: rgb(128, 0, 128); font-family: 微软雅黑;">}</span></pre></div><div style="font-size: 14.6667px; line-height: 17.6px;"><br></div><div><div><div><h1 style="margin-top: 8px; margin-bottom: 8px; padding: 5px 15px; max- 100%; border: 0px currentcolor; border-image-source: initial; border-image-slice: initial; border-image- initial; border-image-outset: initial; border-image-repeat: initial; border-radius: 5px 5px 0px 0px; box-sizing: border-box !important; word-wrap: break-word !important; background-color: rgb(255, 81, 0);"><font color="#ffffff"><span style="font-size: 18.6667px; line-height: 25px;">字符串编码格式转换</span></font></h2></div></div></div><div><title>
    /**
     * 字符串编码格式转换
     * @author 白乾涛
     */
    public class CodeUtils {
        public static void main(String[] args) {
            String string = "aA.1字符串编码格式转换";
            System.out.println(gbk2Unicode(string));
            System.out.println(gbk2utf8(string));
            System.out.println(utf2gbk(string));
            System.out.println(utf2Unicode(string));
            System.out.println(unicode2GBK(string));
            System.out.println(unicode2Utf8(string));
        }
        /**
         * GBK转UTF8
         * @param gbk    GBK编码格式的字符串
         * @return    UTF8编码格式的字符串
         */
        public static String gbk2utf8(String gbk) {
            String l_temp = gbk2Unicode(gbk);
            l_temp = unicode2Utf8(l_temp);
            return l_temp;
        }
        /**
         * UTF8转GBK
         * @param utf    UTF8编码格式的字符串
         * @return    GBK编码格式的字符串
         */
        public static String utf2gbk(String utf) {
            String l_temp = utf2Unicode(utf);
            l_temp = unicode2GBK(l_temp);
            return l_temp;
        }
        /**
         *GBK转Unicode
         * @param str    GBK编码格式的字符串
         * @return String    Unicode编码格式的字符串
         */
        public static String gbk2Unicode(String str) {
            StringBuffer result = new StringBuffer();
            for (int i = 0; i < str.length(); i++) {
                char chr1 = (char) str.charAt(i);
                if ((chr1 & (0x00FF)) == chr1) {
                    result.append(chr1);
                    continue;
                }
                result.append("\u" + Integer.toHexString((int) chr1));
            }
            return result.toString();
        }
        /**
         *Unicode转GBK
         * @param dataStr    Unicode编码格式的字符串
         * @return String    GBK编码格式的字符串
         */
        public static String unicode2GBK(String dataStr) {
            int index = 0;
            StringBuffer buffer = new StringBuffer();
            int li_len = dataStr.length();
            while (index < li_len) {
                if (index >= li_len - 1 || !"\u".equals(dataStr.substring(index, index + 2))) {
                    buffer.append(dataStr.charAt(index));
                    index++;
                    continue;
                }
                String charStr = "";
                charStr = dataStr.substring(index + 2, index + 6);
                char letter = (char) Integer.parseInt(charStr, 16);
                buffer.append(letter);
                index += 6;
            }
            return buffer.toString();
        }
        /**
         * UTF8转Unicode
         * @param inStr    UTF8编码格式的字符串
         * @return        Unicode编码格式的字符串
         */
        public static String utf2Unicode(String inStr) {
            char[] myBuffer = inStr.toCharArray();
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < inStr.length(); i++) {
                UnicodeBlock ub = UnicodeBlock.of(myBuffer[i]);
                if (ub == UnicodeBlock.BASIC_LATIN) {
                    sb.append(myBuffer[i]);
                } else if (ub == UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) {
                    int j = (int) myBuffer[i] - 65248;
                    sb.append((char) j);
                } else {
                    short s = (short) myBuffer[i];
                    String hexS = Integer.toHexString(s);
                    String unicode = "\u" + hexS;
                    sb.append(unicode.toLowerCase());
                }
            }
            return sb.toString();
        }
        /**
         *Unicode转UTF8
         * @param theString        Unicode编码格式的字符串
         * @return String    UTF8编码格式的字符串
         */
        public static String unicode2Utf8(String theString) {
            char aChar;
            int len = theString.length();
            StringBuffer outBuffer = new StringBuffer(len);
            for (int x = 0; x < len;) {
                aChar = theString.charAt(x++);
                if (aChar == '\') {
                    aChar = theString.charAt(x++);
                    if (aChar == 'u') {
                        // Read the xxxx
                        int value = 0;
                        for (int i = 0; i < 4; i++) {
                            aChar = theString.charAt(x++);
                            switch (aChar) {
                            case '0':
                            case '1':
                            case '2':
                            case '3':
                            case '4':
                            case '5':
                            case '6':
                            case '7':
                            case '8':
                            case '9':
                                value = (value << 4) + aChar - '0';
                                break;
                            case 'a':
                            case 'b':
                            case 'c':
                            case 'd':
                            case 'e':
                            case 'f':
                                value = (value << 4) + 10 + aChar - 'a';
                                break;
                            case 'A':
                            case 'B':
                            case 'C':
                            case 'D':
                            case 'E':
                            case 'F':
                                value = (value << 4) + 10 + aChar - 'A';
                                break;
                            default:
                                throw new IllegalArgumentException("Malformed   \uxxxx   encoding.");
                            }
                        }
                        outBuffer.append((char) value);
                    } else {
                        if (aChar == 't') aChar = ' ';
                        else if (aChar == 'r') aChar = ' ';
                        else if (aChar == 'n') aChar = ' ';
                        else if (aChar == 'f') aChar = 'f';
                        outBuffer.append(aChar);
                    }
                } else outBuffer.append(aChar);
            }
            return outBuffer.toString();
        }
    }





查看全文
  • 相关阅读:
    基于域名的虚拟主机
    用户认证
    部署lnmp
    django开发流程
    sed 和awk的执行方式
    将文本行倒序排列
    《深入理解JAVA虚拟机》----------第二章 JAVA内存区域与内存溢出异常,笔记(上)
    《深入理解JAVA虚拟机》----------第三章 垃圾收集器与内存分配策略,笔记(下)
    《深入理解JAVA虚拟机》----------第三章 垃圾收集器与内存分配策略,笔记(上)
    洛谷P3783 [SDOI2017]天才黑客(前后缀优化建图+虚树+最短路)
  • 原文地址:https://www.cnblogs.com/baiqiantao/p/a7d6b6423726a0910654d61afcef7114.html
  • Copyright © 2011-2022 走看看