java 基础三---数据类型转换

一、 byte[]  和 char[]  相互转换

1.byte[] 转 chaar[]
 public static char[] bytesTochars(byte[] c,int size) {
        if(c==null || c.length==0)
            return null;
          
        char[] b=new char[size];
        try {
            for (int i = 0; i < size; i++) {
             b[i]= (char)(c[i]&0xFF);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return b;
    }

2.char[] 转 byte[]
public static byte[] charsTobytes(char[] c,int size) {
		if(c==null || c.length==0)
			return null;
	      
		byte[] b=new byte[size];
		try {
			for (int i = 0; i < size; i++) {
			 b[i]= (byte)((int)c[i]);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return b;
	}

二、十六进制  和 char[]  相互转换

1.char[]转 hex 
 public static String chars2HexString(char[] c, int size) {
        StringBuffer sb=new StringBuffer();
        try {
            int j = 0;
            for (int i = 0; i < size; i++) {
                String hex = Integer.toHexString(c[i]);
                if (hex.length() == 1) {
                    hex = "0" + hex;
                }
                sb.append(hex.toUpperCase());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sb.toString();
    }

2.hex 转char[]
 public static char[] hexString2Chars(String hex) {
         if(hex!=null && hex.length()>0 && hex.length()%2==0) {
             hex = hex.replace(" ", "");
             char[] bytes = new char[hex.length() / 2];
             for (int i = 0; i < bytes.length; i++) {
                 bytes[i] = (char) Integer.parseInt(hex.substring(2 * i, 2 * i + 2), 16);
             }
             return bytes;
         }
         return null;
    }

三、十六进制 和 byte[]

一、hex 转byte[]
   public static byte[] hexStringToBytes(String hexString) {
        if (hexString == null || hexString.equals("")) {
            return null;
        }
        hexString = hexString.toUpperCase();
        int length = hexString.length() / 2;
        byte[] d = new byte[length];
        for (int i = 0; i < length; i++) {
            int pos = i * 2;
            d[i] = (byte) Integer.parseInt(hexString.substring(pos,pos+2),16);
        }
        return d;
    }

二、 byte[]转 hex
public static String bytes2HexString2(byte[] b, int size) {
		StringBuffer sb=new StringBuffer();

		try {
			for (int i = 0; i < size; i++) {
				String hex = Integer.toHexString(b[i] & 0xFF);
				if (hex.length() == 1) {
					hex = "0" + hex;
				}
				sb.append(hex.toUpperCase());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return sb.toString();
	}

 

四、int 和 byte[]

1.int 转 byte[]
public static byte[] int2Bytes(int num) {  
        byte[] byteNum = new byte[4];  
        for (int ix = 0; ix < 4; ++ix) {  
            int offset = 32 - (ix + 1) * 8;  
            byteNum[ix] = (byte) ((num >> offset) & 0xff);  
        }  
        return byteNum;  
}

2.byte[] 转int	 
public static int bytesToInt(byte[] bytes) {
		int addr = bytes[0] & 0xFF;
		addr |= ((bytes[1] << 8) & 0xFF00);
		addr |= ((bytes[2] << 16) & 0xFF0000);
		addr |= ((bytes[3] << 24) & 0xFF000000);
		return addr;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值