java 字符转换

package code;


public class TypeConversion {


//
//    private static byte uniteBytes(byte tmp, byte tmp2) {
//        byte b0 = Byte.decode("0x" + tmp).byteValue();
//        b0 = (byte) (b0 << 4);
//        byte b1 = Byte.decode("0x" + tmp2).byteValue();
//        byte ret = (byte) (b0 | b1);
//        return ret;
//    }
    
  /**
     * 将两个ASCII字符合成一个字节; 如:"EF"–> 0xEF
     * 
     * @param src0
     *            byte
     * @param src1
     *            byte
     * @return byte
     */
    public static byte uniteBytes(byte src0, byte src1) {
        byte _b0 = Byte.decode("0x" + new String(new byte[] {src0})).byteValue();
        _b0 = (byte) (_b0 << 4);
        byte _b1 = Byte.decode("0x" + new String(new byte[] { src1 })).byteValue();
        byte ret = (byte) (_b0 ^ _b1);
        return ret;
    }
/** 
* 将指定字符串src,以每两个字符分割转换为16进制形式 
* 如:"2B44EFD9" --> byte[]{0x2B, 0x44, 0xEF, 0xD9} 
* @param src String 
* @return byte[] 
*/ 
public static byte[] INTHexString2Bytes(String src){ 
byte[] ret = new byte[8]; 
byte[] tmp = src.getBytes(); 

for(int i=0; i<8; i++)

ret[i] = uniteBytes(tmp[i*2], tmp[i*2+1]); 

return ret; 




    /** 
     * @Title:bytes2HexString 
     * @Description:字节数组转16进制字符串 
     * @param b 
     *            字节数组 
     * @return 16进制字符串 
     * @throws 
     */  
    public static String bytes2HexString(byte[] b) {  
        StringBuffer result = new StringBuffer();  
        String hex;  
        for (int i = 0; i < b.length; i++) {  
            hex = Integer.toHexString(b[i] & 0xFF);  
            if (hex.length() == 1) {  
                hex = '0' + hex;  
            }  
            result.append(hex.toUpperCase());  
        }  
        return result.toString();  
    }  

    
    /** 
     * @Title:hexString2Bytes 
     * @Description:16进制字符串转字节数组 
     * @param src 
     *            16进制字符串 
     * @return 字节数组 
     * @throws 
     */  
    public static int[] hexString2IntBytes(String src) {  
        int l = src.length() / 2;  
        int[] ret = new int[l];  
        for (int i = 0; i < l; i++) {  
            ret[i] = (char) Integer  
                    .valueOf(src.substring(i * 2, i * 2 + 2), 16).shortValue();  
        }  
        return ret;  
    }  
    
    
    /** 
     * @Title:hexString2Bytes 
     * @Description:16进制字符串转字节数组 
     * @param src 
     *            16进制字符串 
     * @return 字节数组 
     * @throws 
     */  
    public static byte[] hexString2Bytes(String src) {  
        int l = src.length() / 2;  
        byte[] ret = new byte[l];  
        for (int i = 0; i < l; i++) {  
            ret[i] = (byte) Integer  
                    .valueOf(src.substring(i * 2, i * 2 + 2), 16).byteValue();  
        }  
        return ret;  
    }  
  
    
    /** 
     * @Title:string2HexString 
     * @Description:字符串转16进制字符串 
     * @param strPart 
     *            字符串 
     * @return 16进制字符串 
     * @throws 
     */  
    public static String string2HexString(String strPart) {  
        StringBuffer hexString = new StringBuffer();  
        for (int i = 0; i < strPart.length(); i++) {  
            int ch = (int) strPart.charAt(i);  
            String strHex = Integer.toHexString(ch);  
            hexString.append(strHex);  
        }  
        return hexString.toString();  
    }
        
        /** 
         * @Title:char2Byte 
         * @Description:字符转成字节数据char-->integer-->byte 
         * @param src 
         * @return 
         * @throws 
         */  
        public static Byte char2Byte(Character src) {  
            return Integer.valueOf((int)src).byteValue();  
        }  
       
        /** 
         * @Title:hexString2String 
         * @Description:16进制字符串转字符串 
         * @param src 
         *            16进制字符串 
         * @return 字节数组 
         * @throws 
         */  
        public static String hexString2String(String src) {  
            String temp = "";  
            for (int i = 0; i < src.length() / 2; i++) 
            {  
                temp = temp + (char) Integer.valueOf(src.substring(i * 2, i * 2 + 2), 16).byteValue();                                                 
            }  
            return temp;  
        }  

  public static void main(String args[]) {  
       System.out.println(hexString2String("3236"));  
       
       
       int[] intcovbyte = new int[10];
       byte[] covbyte = new byte[10];
       String str = new String("3231d5");
       int num =0;
       covbyte = hexString2Bytes(str);
       System.out.println(covbyte[0]);
       System.out.println(covbyte[1]);
      
       System.out.println(covbyte[2]);
       
       
     
  
       intcovbyte =  hexString2IntBytes("d5323435");
       
       System.out.println("int类型:");
       System.out.println(intcovbyte[0]);
       System.out.println(intcovbyte[1]);
      
       System.out.println(intcovbyte[2]);
       
       
   }  


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值