hex字符串与byte数组互转


Toolkit类中的工具方法

package org.sl.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.security.MessageDigest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

public class Toolkit {
	public static final File logFile = new File(".", "app_sl.log");
	public static final SimpleDateFormat dateFormat = new SimpleDateFormat("[yyyy/MM/dd HH:mm:ss]");
//	/**
//	 * 8位长的随机码
//	 */
//	static final String secret8 = "Iv}7xlJ^";
	
	
	/**
	 * logger 
	 * @param log 日志内容
	 * @param std 是否向控制台打印
	 */
	public static void logger(String log, boolean std){
		OutputStream out = null;
		String dt = null;
		
		try{
			dt = dateFormat.format(new Date());
			
			if(std) System.out.println(dt+log);
			
			if(logFile.length()>1024*1024*20){
				logFile.delete();
			}
			out = new FileOutputStream(logFile, true);			
			out.write(dt.getBytes());
			out.write(log.getBytes());
			out.write('\n');
			out.flush();
		}catch(Exception ex){			
		}finally{
			try {
				if(null!=out)out.close();
			} catch (Exception e) {			
			}
		}
	}	
		
	/**
	 * logger
	 * @param log
	 */
	public static void logger(String log){		
		logger(log, false);
	}
	
//	/**
//	 * 异或加密	
//	 * @param src
//	 * @return
//	 */
//	public static byte[] xor(byte[] src){
//		byte[] res = new byte[src.length];
//		
//		for(int i=0; i
	 * 2个hex转为一个byte
	 * @param src
	 * @return
	 */
	public static byte[] hex2Bytes1(String src){
		byte[] res = new byte[src.length()/2];
		char[] chs = src.toCharArray();
		int[] b = new int[2];

		for(int i=0,c=0; i='0' && chs[i+j]<='9'){
					b[j] = (chs[i+j]-'0');
				}else if(chs[i+j]>='A' && chs[i+j]<='F'){
					b[j] = (chs[i+j]-'A'+10);
				}else if(chs[i+j]>='a' && chs[i+j]<='f'){
					b[j] = (chs[i+j]-'a'+10);
				}
			} 
			
			b[0] = (b[0]&0x0f)<<4;
			b[1] = (b[1]&0x0f);
			res[c] = (byte) (b[0] | b[1]);
		}
		
		return res;
	}
	
	/**
	 * hex字符串转byte数组
* 2个hex转为一个byte * @param src * @return */ public static byte[] hex2Bytes(String src){ byte[] res = new byte[src.length()/2]; char[] chs = src.toCharArray(); for(int i=0,c=0; i * 一个byte转为2个hex字符 * @param src * @return */ public static String bytes2Hex(byte[] src){ char[] res = new char[src.length*2]; final char hexDigits[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'}; for(int i=0,j=0; i>>4 & 0x0f]; res[j++] = hexDigits[src[i] & 0x0f]; } return new String(res); } /** * 生成安全签字 * @param src * @return */ public static String signature(String src){ final String secret8 = "Iv}7xlJ^"; return sha1Hex(secret8+src); } /** * 返回16进制sha-1加密后信息 * @param btInput * @return */ public static String sha1Hex(byte[] btInput){ final char hexDigits[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'}; try { MessageDigest mdInst = MessageDigest.getInstance("SHA-1"); mdInst.update(btInput); byte[] md = mdInst.digest(); // 把密文转换成十六进制的字符串形式 int j = md.length; char str[] = new char[j * 2]; for (int i=0,k=0; i < j; i++) { byte byte0 = md[i]; str[k++] = hexDigits[byte0 >>> 4 & 0xf]; str[k++] = hexDigits[byte0 & 0xf]; } return new String(str); } catch (Exception e) { return ""; } } /** * 返回16进制sha-1加密后信息 * @param src * @return */ public static String sha1Hex(String src){ return sha1Hex(src.getBytes()); } }


测试类:

      static void t16(){
		byte[] src = new byte[1024];
		new Random().nextBytes(src);
		byte[] digest = digest(src,0,src.length);
//		digest[3]='2';
		System.out.println(checkDigest(digest, src,0,src.length));
	}

      static void t15(){
		byte[] src = new byte[10];
		new Random().nextBytes(src);
//		byte[] src = "12345qwert".getBytes();
		for(byte b: src){
			System.out.print((byte)b+" ");
		}
		System.out.println("----src---");
		String hex = Toolkit.bytes2Hex(src);
		System.out.println(hex);
		
		src = Toolkit.hex2Bytes1(hex);
		for(byte b: src){
			System.out.print(b+" ");
		}
		System.out.println("----src---");
		hex = Toolkit.bytes2Hex(src);
		System.out.println(hex);
		
		src = Toolkit.hex2Bytes1(hex);
		for(byte b: src){
			System.out.print(b+" ");
		}
		System.out.println("----src---");
	}





你可能感兴趣的:(hex字符串与byte数组互转)