TCP中一些类型转换。16进制转字符串、十进制以及截取字符串

自己项目中遇到的,做一些记录,

 /**
	 * 16进制字节转10进制数组。
	 * @param src
	 * @return
	 */
	 public static String bytesToHexString(byte[] src){  
		    StringBuilder stringBuilder = new StringBuilder("");  
		    if (src == null || src.length <= 0) {  
		        return null;  
		    }  
		    for (int i = 0; i < src.length; i++) {  
		        int v = src[i] & 0xFF;  
		        stringBuilder.append(v);
		    }  
		    return stringBuilder.toString();  
		}  
	 
	 /**
	  * 截取字节数组中的固定长度数值
	  * @param src
	  * @param begin
	  * @param count
	  * @return
	  */
	 public static byte[] subBytes(byte[] src, int begin, int count) {  
		    byte[] bs = new byte[count];  
		    System.arraycopy(src, begin, bs, 0, count);  
		    return bs;  
		}  
	 /**
	  * 16进制字节数组转字符串
	  * @param b
	  * @return
	  */
	 public static String toHexString1(byte[] b){
	        StringBuffer buffer = new StringBuffer();
	        for (int i = 0; i < b.length; ++i){
	            buffer.append(toHexString1(b[i]));
	        }
	        return buffer.toString();
	    }
	  public static String toHexString1(byte b){
	        String s = Integer.toHexString(b & 0xFF);
	        if (s.length() == 1){
	            return "0" + s;
	        }else{
	            return s;
	        }
	    }

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