Java实现十六进制或Unicode与字符串之间的互相转化

来源:互联网 发布:宝宝起名软件正式版 编辑:程序博客网 时间:2024/04/29 13:47

From:http://blog.csdn.net/killerlegend/article/details/27705323

Author:KillerLegend

Date:2014.5.30

~~~~~~~~~~~~~~~~~~~分割线~~~~~~~~~~~~~~~~~~~~~

Unicode与字符串之间的相互转化

/*Author:KillerLegendDate:2014.2.10函数功能:实现字符串和Unicode编码之间的相互转换*/import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.IOException;import java.io.InputStreamReader;import java.io.OutputStreamWriter;public class uc{public static void main(String[] args) throws IOException{ BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));     //BufferedWriter bufw = new BufferedWriter(new OutputStreamWriter(System.out));     int select;     System.out.println("1:字符(串)==>Unicode编码(串) 2:Unicode编码(串)==>字符(串) 3:结束");     while(true)         {     System.out.println("请选择序号:");     select = Integer.parseInt(bufr.readLine());    if(1==select)    {    System.out.println(str2unicode(bufr.readLine()));    }    else if(2==select)    {        System.out.println(unicode2str(bufr.readLine()));    }    else    {    System.out.println("Goodbye Baby!");    break;    }  }}//字符(串)==>Unicode编码(串)public static String str2unicode(String str){char[] strArr = str.toCharArray();String unicode = "",temp="";for(int index=0;index<strArr.length;++index){ temp=Integer.toHexString(strArr[index]).toUpperCase(); while(temp.length()-4!=0) temp="0"+temp; unicode+="\\u"+temp;}return unicode;}//Unicode编码(串)==>字符(串)public static String unicode2str(String unicodeStr) {int beg=0,end=0;String subStr="";String result="";while(beg!=-1){end=unicodeStr.indexOf("\\u", beg+2);subStr=(-1==end)?unicodeStr.substring(beg+2,unicodeStr.length()):unicodeStr.substring(beg+2, end);result+=String.valueOf((char)Integer.parseInt(subStr, 16));beg=end;}return result;}}

十六进制和字符串之间的相互转化

/*Author:KillerLegendDate:2014.5.30函数功能:实现字符串和十六进制之间的相互转换*/import java.io.*; public class hex {    public static void main(String[] args) throws IOException    {        BufferedReader bufread = new BufferedReader(new InputStreamReader(System.in));        System.out.println("1:字符串转换为十六进制转换 2:十六进制转换为字符串 3:退出");        while(true)        {        String tmp=null;        int select = Integer.parseInt(bufread.readLine());        if(1==select)        {        System.out.println("请输入字符串:");        tmp = bufread.readLine();        System.out.println(Str2Hex(tmp));        }        else if(2==select)        {        System.out.println("请输入十六进制:");        tmp = bufread.readLine();        System.out.println(Hex2Str(tmp));        }        else        {        System.out.println("Goodbye Baby!");        break;        }        System.out.println("1:字符串转换为十六进制转换 2:十六进制转换为字符串 3:退出");        }    }              /*     * 字符串---->16进制数字     */    public static String Str2Hex(String str)     {    String HexStr="0123456789ABCDEF";        byte[] bytes = str.getBytes();//由默认编码获取字节流数组        StringBuilder sb = new StringBuilder(bytes.length<<1);        for (int i = 0; i < bytes.length; i++)         {        int head=(bytes[i] & 0xf0) >> 4;//高四位        int tail=bytes[i] & 0x0f;//低四位            sb.append(HexStr.charAt(head));            sb.append(HexStr.charAt(tail));        }        return sb.toString();    }     /*     * 16进制数字---->字符串     */    public static String Hex2Str(String bytes)    {    String HexStr="0123456789ABCDEF";        ByteArrayOutputStream baos = new ByteArrayOutputStream(bytes.length()>>1);        // 每2位16进制整数组成一个字节        for (int i = 0; i < bytes.length(); i += 2)        {        int cond1=HexStr.indexOf(bytes.charAt(i))<< 4;        int cond2=HexStr.indexOf(bytes.charAt(i + 1));        baos.write(cond1|cond2);        }        return new String(baos.toByteArray());    }}



0 0
原创粉丝点击