编码转化

本文深入讲解了各种进制之间的转换方法,包括Unicode与字符串、数字与16进制、ASCII与16进制字符串的相互转换,以及10进制到BCD码的转换过程。提供了详细的代码实现,适合程序员理解和掌握不同场景下的进制转换技巧。

进制转化

unicode转成字符串

    public static String revert(String str) {
        str = (str == null ? "" : str);
        if (str.indexOf( "\\u" ) == -1)//如果不是unicode码则原样返回 
            return str;

        StringBuffer sb = new StringBuffer( 1000 );

        for (int i = 0; i < str.length() - 6; ) {
            String strTemp = str.substring( i, i + 6 );
            String value = strTemp.substring( 2 );
            int c = 0;
            for (int j = 0; j < value.length(); j++) {
                char tempChar = value.charAt( j );
                int t = 0;
                switch (tempChar) {
                    case 'a':
                        t = 10;
                        break;
                    case 'b':
                        t = 11;
                        break;
                    case 'c':
                        t = 12;
                        break;
                    case 'd':
                        t = 13;
                        break;
                    case 'e':
                        t = 14;
                        break;
                    case 'f':
                        t = 15;
                        break;
                    default:
                        t = tempChar - 48;
                        break;
                }

                c += t * ((int) Math.pow( 16, (value.length() - j - 1) ));
            }
            sb.append( (char) c );
            i = i + 6;
        }
        return sb.toString();
    }

字符串转成unicode

    public static String convert(String str) {
        str = (str == null ? "" : str);
        String tmp;
        StringBuffer sb = new StringBuffer( 1000 );
        char c;
        int i, j;
        sb.setLength( 0 );
        for (i = 0; i < str.length(); i++) {
            c = str.charAt( i );
            sb.append( "\\u" );
            j = (c >>> 8); //取出高8位 
            tmp = Integer.toHexString( j );
            if (tmp.length() == 1) sb.append( "0" );
            sb.append( tmp );
            j = (c & 0xFF); //取出低8位 
            tmp = Integer.toHexString( j );
            if (tmp.length() == 1) sb.append( "0" );
            sb.append( tmp );

        }
        return (new String( sb ));
    }

将数字转16进制

public static String intTohex(int n) {
        StringBuffer s = new StringBuffer();
        String a;
        char[] b = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
        while (n != 0) {
            s = s.append( b[n % 16] );
            n = n / 16;
        }
        a = s.reverse().toString();
        if ("".equals( a )) {
            a = "00";
        }
        if (a.length() == 1) {
            a = "0" + a;
        }
        return a;
    }

16进制转10进制

    public static byte[] hexToByteArray(String inHex) {
        int hexlen = inHex.length();
        byte[] result;
        if (hexlen % 2 == 1) {
            //奇数
            hexlen++;
            result = new byte[(hexlen / 2)];
            inHex = "0" + inHex;
        } else {
            //偶数
            result = new byte[(hexlen / 2)];
        }
        int j = 0;
        for (int i = 0; i < hexlen; i += 2) {
            result[j] = hexToByte( inHex.substring( i, i + 2 ) );
            j++;
        }
        return result;
    }

将16进制的字符串 hex转ascii字符串

public static String hexStr2Str(String hexStr) {
        String str = "0123456789ABCDEF";
        char[] hexs = hexStr.toCharArray();
        byte[] bytes = new byte[hexStr.length() / 2];
        int n;
        for (int i = 0; i < bytes.length; i++) {
            n = str.indexOf( hexs[2 * i] ) * 16;
            n += str.indexOf( hexs[2 * i + 1] );
            bytes[i] = (byte) (n & 0xff);
        }
        return new String( bytes );
    }

@参数: 10进制串
@结果: BCD码

   public static byte[] str2Bcd(String asc) {
       int len = asc.length();
       int mod = len % 2;
       if (mod != 0) {
           asc = "0" + asc;
           len = asc.length();
       }
       byte abt[] = new byte[len];
       if (len >= 2) {
           len = len / 2;
       }
       byte bbt[] = new byte[len];
       abt = asc.getBytes();
       int j, k;
       for (int p = 0; p < asc.length() / 2; p++) {
           if ((abt[2 * p] >= '0') && (abt[2 * p] <= '9')) {
               j = abt[2 * p] - '0';
           } else if ((abt[2 * p] >= 'a') && (abt[2 * p] <= 'z')) {
               j = abt[2 * p] - 'a' + 0x0a;
           } else {
               j = abt[2 * p] - 'A' + 0x0a;
           }
           if ((abt[2 * p + 1] >= '0') && (abt[2 * p + 1] <= '9')) {
               k = abt[2 * p + 1] - '0';
           } else if ((abt[2 * p + 1] >= 'a') && (abt[2 * p + 1] <= 'z')) {
               k = abt[2 * p + 1] - 'a' + 0x0a;
           } else {
               k = abt[2 * p + 1] - 'A' + 0x0a;
           }
           int a = (j << 4) + k;
           byte b = (byte) a;
           bbt[p] = b;
       }
       return bbt;
   }

将InputStream先转为byte【】

    public static final byte[] input2byte(InputStream inStream) throws IOException {
        ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
        byte[] buff = new byte[100];
        int rc = 0;
        while ((rc = inStream.read( buff, 0, 100 )) > 0) {
            swapStream.write( buff, 0, rc );
        }
        byte[] in2b = swapStream.toByteArray();
        return in2b;
    }

将byte【】转为16进制

    public static String bytesToHexString(byte[] src) {
        StringBuffer sb = new StringBuffer( "" );
        if (src == null || src.length <= 0) {
            return null;
        }
        for (int i = 0; i < src.length; i++) {
            int v = src[i] & 0xFF;
            String hv = Integer.toHexString( v ).toUpperCase();
            if (hv.length() < 2) {
                sb.append( 0 );
            }
            sb.append( hv );
            if (i != src.length - 1) {
                sb.append( " " );
            }
        }
        return sb.toString();
    }
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值