Unicode 码和汉字,十进制之间互转!二进制、十六进制,十进制的转换

Unicode 码和汉字,十进制之间互转!二进制、十六进制,十进制的转换

用法文档这里就不介绍了!直接上代码

package org.transition;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringAndUnicode {

	
	public static void main(String[] args) {
		String s="加务劢";
       System.out.println("加务劢对应unicode码:"+strToUnicode(s));//把汉字转成UNICODE
        //unioncde 转汉字
       System.out.println("unioncde 转汉字:"+unicodeToStr("\u52a0\u52a1\u52a2"));
        //把unicode 转十进制  \u52a0=21152
       int dec=hexToDic("\\u52a0");
       System.out.println("unicode转为十进制:"+dec);
        
        
    }
	
	/**
	 * 把汉字转成UNICODE
	 */
	private static String strToUnicode(String s) {
		String str = "";
        for (int i = 0; i < s.length(); i++) {
            int ch = (int) s.charAt(i);
            if (ch > 255)
                str += "\\u" + Integer.toHexString(ch);
	           else
                str += "\\" + Integer.toHexString(ch);
        }
        
       return str;
	}
		
	
	/**
	 * unicode 转成汉字
	 */
	
	public static String unicodeToStr(String str){
		//将给定的正则表达式编译到模式中。
		//\\u 以便在从文件或键盘击键读取的表达式中使用 Unicode 转义
		// \p{XDigit} 十六进制数字:[0-9a-fA-F]
		Pattern p=Pattern.compile("(\\\\u(\\p{XDigit}{4}))");
	    //创建匹配给定输入与此模式的匹配器。
		Matcher m=p.matcher(str);
		
		char c;
		//方法扫描输入序列以查找与该模式匹配的下一个子序列。
		while(m.find()){
			//返回在以前匹配操作期间由给定组捕获的输入子序列。
			String temp=m.group(2);
			c=(char)Integer.parseInt(temp,16);
			str = str.replace(m.group(1), c + "");  
		}
		
		return str;
	}
	
	/**
	 * 4个16进制快速转10进制 ,这个是方便自已算 自已写的,肯定会有不足
	 */
	
	public static int hexToDic(String hex){
		
		hex=hex.toUpperCase();
		//去除\\u 参数一定要加上转义附\ 要不然会自动转换为有效汉字的
		if("\\U".equals(hex.substring(0,2)))
			hex=hex.substring(2);
		
		int res=0;
		Character [] charArray=new Character[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9','A','B','C','D','E','F'};
		
		char[] charAr=hex.toCharArray();
		if(charAr.length!=4){
			System.out.println("该方法只转换4位16进制 用于计算unicode 对应的 十进");
		return 0;
		}
		
		int pow=0;
		for(int i=0;i<4;i++){
			switch (i) {
			case 0:pow=3;break;
			case 1:pow=2;break;
			case 2:pow=1;break;
			case 3:pow=0;break;
			}
			
			Character c=charAr[i];
			for(int j=0;j<charArray.length;j++){
				char charHex=charArray[j];
				//取出字母对应的数字
				if(c.equals(charHex)){
					//得到次方合
					int powRes=(int) Math.pow(16, pow);
					//具体数字十六进制数*次方和=
					res+=j*powRes;
				}
			}
		}
		return res;
	}

}

===============================

二进制,十进制,十六进制转换!以下代码是转载的!简单测试了几个,还行

package org.rui.algorithm;
public class StrBinaryTurn {
	
	
	
	
	public static void main(String[] args) {
		String abc="abc";
		
		StrBinaryTurn sbt=new StrBinaryTurn();
		String bin=sbt.StrToBinstr(abc);
		System.out.println("bin:"+bin);
		
		String str=sbt.BinstrToStr(bin);
		System.out.println("str:"+str);
	
		
	}
	
    //将Unicode字符串转换成bool型数组
    public boolean[] StrToBool(String input){
        boolean[] output=Binstr16ToBool(BinstrToBinstr16(StrToBinstr(input)));
        return output;
    }
    //将bool型数组转换成Unicode字符串
    public String BoolToStr(boolean[] input){
        String output=BinstrToStr(Binstr16ToBinstr(BoolToBinstr16(input)));
        return output;
    }
    //将字符串转换成二进制字符串,以空格相隔
    public String StrToBinstr(String str) {
        char[] strChar=str.toCharArray();
        String result="";
        for(int i=0;i<strChar.length;i++){
            result +=Integer.toBinaryString(strChar[i])+ " ";
        }
        return result;
    }
    //将二进制字符串转换成Unicode字符串
    public String BinstrToStr(String binStr) {
        String[] tempStr=StrToStrArray(binStr);
        char[] tempChar=new char[tempStr.length];
        for(int i=0;i<tempStr.length;i++) {
            tempChar[i]=BinstrToChar(tempStr[i]);
        }
        return String.valueOf(tempChar);
    }
    //将二进制字符串格式化成全16位带空格的Binstr
    private String BinstrToBinstr16(String input){
        StringBuffer output=new StringBuffer();
        String[] tempStr=StrToStrArray(input);
        for(int i=0;i<tempStr.length;i++){
            for(int j=16-tempStr[i].length();j>0;j--)
                output.append('0');
            output.append(tempStr[i]+" ");
        }
        return output.toString();
    }
    //将全16位带空格的Binstr转化成去0前缀的带空格Binstr
    private String Binstr16ToBinstr(String input){
        StringBuffer output=new StringBuffer();
        String[] tempStr=StrToStrArray(input);
        for(int i=0;i<tempStr.length;i++){
            for(int j=0;j<16;j++){
                if(tempStr[i].charAt(j)=='1'){
                    output.append(tempStr[i].substring(j)+" ");
                    break;
                }
                if(j==15&&tempStr[i].charAt(j)=='0')
                    output.append("0"+" ");
            }
        }
        return output.toString();
    }   
    //二进制字串转化为boolean型数组  输入16位有空格的Binstr
    private boolean[] Binstr16ToBool(String input){
        String[] tempStr=StrToStrArray(input);
        boolean[] output=new boolean[tempStr.length*16];
        for(int i=0,j=0;i<input.length();i++,j++)
            if(input.charAt(i)=='1')
                output[j]=true;
            else if(input.charAt(i)=='0')
                output[j]=false;
            else
                j--;
        return output;
    }
    //boolean型数组转化为二进制字串  返回带0前缀16位有空格的Binstr
    private String BoolToBinstr16(boolean[] input){ 
        StringBuffer output=new StringBuffer();
        for(int i=0;i<input.length;i++){
            if(input[i])
                output.append('1');
            else
                output.append('0');
            if((i+1)%16==0)
                output.append(' ');           
        }
        output.append(' ');
        return output.toString();
    }
    //将二进制字符串转换为char
    private char BinstrToChar(String binStr){
        int[] temp=BinstrToIntArray(binStr);
        int sum=0;   
        for(int i=0; i<temp.length;i++){
            sum +=temp[temp.length-1-i]<<i;
        }   
        return (char)sum;
    }
    //将初始二进制字符串转换成字符串数组,以空格相隔
    private String[] StrToStrArray(String str) {
        return str.split(" ");
    }
    //将二进制字符串转换成int数组
    private int[] BinstrToIntArray(String binStr) {       
        char[] temp=binStr.toCharArray();
        int[] result=new int[temp.length];  
        
        for(int i=0;i<temp.length;i++) {
            result[i]=temp[i]-48;
        }
        return result;
    }
   

   
}

    原文作者:进制转换
    原文地址: https://blog.csdn.net/liangrui1988/article/details/19215633
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞