package com.sun;

public class Snippet {
    public static void main(String[] args) {
        String cn = "你";
        System.out.println(cnToUnicode(cn));
        // 字符串 : \u5f00\u59cb\u4efb\u52a1 ,由于 \ 在java里是转义字符,要写出下面这种形式
        String unicode = "\\u4f60";
        System.out.println(unicodeToCn(unicode));
    }
    
    private static String unicodeToCn(String unicode) {
        /** 以 \ u 分割,因为java注释也能识别unicode,因此中间加了一个空格*/
        String[] strs = unicode.split("\\\\u");
        String returnStr = "";
        // 由于unicode字符串以 \ u 开头,因此分割出的第一个字符是""。
        for (int i = 1; i < strs.length; i++) {
          returnStr += (char) Integer.valueOf(strs[i], 16).intValue();
        }
        return returnStr;
    }
    
    private static String cnToUnicode(String cn) {
        char[] chars = cn.toCharArray();
        String returnStr = "";
        for (int i = 0; i < chars.length; i++) {
          returnStr += "\\u" + Integer.toString(chars[i], 16);
        }
        return returnStr;
    }
}

效果:

java 中文转Unicode 以及 Unicode转中文的更多相关文章

  1. java 中文转换成Unicode编码和Unicode编码转换成中文

    转自:一叶飘舟 http://blog.csdn.net/jdsjlzx/article/details/ package lia.meetlucene; import java.io.IOExcep ...

  2. java对含有中文的字符串进行Unicode编码

    public class MyUtil { public static void main(String[] args) throws Exception { String s = "a中a ...

  3. 中文字符串转换为十六进制Unicode编码字符串

    package my.unicode; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Uni ...

  4. js 中文汉字转Unicode、Unicode转中文汉字、ASCII转换Unicode、Unicode转换ASCII、中文转换&#XXX函数代码

    最近看不少在线工具里面都有一些编码转换的代码,很多情况下我们都用得到,这里脚本之家小编就跟大家分享一下这些资料 Unicode介绍 Unicode(统一码.万国码.单一码)是一种在计算机上使用的字符编 ...

  5. 解决JSON.stringify()自动将中文转译成unicode的方法

    最近在工作中,发现在IE8下JSON.stringify()自动将中文转译为unicode编码,原本选择的中文字符,传到后台变为了unicode编码,即\u****的形式.查找资料后发现,与标准的JS ...

  6. "上市时间:&nbsp;2014&#24180;&#31179;&#20908;&#23395;" unicode十进制编码转中文

    "上市时间: 2014年秋冬季" unicode十进制编码转中文 System.Web.HttpUtility.HtmlDecode(tmp);

  7. jmeter接口测试--响应结果Unicode转码成中文

    jmeter接口测试-响应结果Unicode转码成中文 一般情况下,接口返回数据都会经过加密,所以有时相应结果会显示为Unicode,因此,需添加BeanShell PostProcessor,加入代 ...

  8. 编码对象或者字串中包含Unicode字符怎样转换为中文

    In [18]: c = '你好' In [20]: d = c.encode('unicode_escape') In [21]: d Out[21]: b'\\u4f60\\u597d' In [ ...

  9. Java 16进制、unicode互转

    package service; import java.util.regex.Matcher; import java.util.regex.Pattern; public class CodeCh ...

随机推荐

  1. Tomcat学习笔记(二)

    Servlet浅析 javax.servlet.Servlet是一个接口,所有的Servlet必须实现接口里面的方法. 该接口在tomcat/bin中的servlet-api.jar包中. Servl ...

  2. HDU 1087 最大上升子序列的和

    Super Jumping! Jumping! Jumping! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 ...

  3. code forces 996D Suit and Tie

    D. Suit and Tie time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...

  4. code forces 999C Alphabetic Removals

    C. Alphabetic Removals time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  5. 转 linux下cat命令详解

    linux下cat命令详解 http://www.cnblogs.com/perfy/archive/2012/07/23/2605550.html 简略版: cat主要有三大功能:1.一次显示整个文 ...

  6. 使用aiohttp的一个小例子

    #!/usr/bin/env python # encoding: utf-8 import aiohttp import asyncio import traceback import time i ...

  7. 牛客网 牛客练习赛7 B.购物-STL(priority_queue)

    B.购物 时间限制:C/C++ 1秒,其他语言2秒空间限制:C/C++ 32768K,其他语言65536K64bit IO Format: %lld 题目描述 在遥远的东方,有一家糖果专卖店 这家糖果 ...

  8. Python的网络编程[1] -> FTP 协议[2] -> 使用 ftplib 建立 FTP 客户端

    使用 ftplib 建立 FTP 客户端 用于建立FTP Client,与 pyftplib 建立的 Server 进行通信. 快速导航 1. 模块信息 2. 建立 FTP 客户端 1. 模块信息 1 ...

  9. 在sae中运行web.py应用

    sae 是新浪推出的PaaS业务,可以提供免运维的容器服务,官方网站( https://www.sinacloud.com/ ) 假设您已经在本地开发好了web.py 应用,您可以通过github客户 ...

  10. java.sql.SQLException: Access denied for user 'roo'@'localhost' (using password: YES)

    初学mysql,安装了mysql8.0.11,激动的用jdbc连接数据库,出现error,折腾了三天依旧无解,最后无奈装了比较稳定的mysql5.5,问题得以解决,很迷,但只要error没了就开心. ...