首页  

java hex util     所属分类 java 浏览量 468
public class HexUtil {

	// 字节数组转成十六进制表示
	public static String encode(byte[] src) {
		String strHex = "";
		StringBuilder sb = new StringBuilder("");
		for (int n = 0; n < src.length; n++) {
			strHex = Integer.toHexString(src[n] & 0xFF);
			// 每个字节 8位 16进制 4位 0~9 abcdef
			// 一个字节 用 2个 16进制字符表示 ,位数不够,高位补0
			sb.append((strHex.length() == 1) ? "0" + strHex : strHex);
		}
		return sb.toString().trim();
	}

	// 字符串转成字节数组
	public static byte[] decode(String src) {
		int m = 0, n = 0;
		//
		int byteLen = src.length() / 2;
		byte[] ret = new byte[byteLen];
		for (int i = 0; i < byteLen; i++) {
			m = i * 2 + 1;
			n = m + 1;
			int intVal = Integer.decode("0x" + src.substring(i * 2, m) + src.substring(m, n));
			ret[i] = Byte.valueOf((byte) intVal);
		}
		return ret;
	}

	public static void main(String[] args) throws Exception {
		String str = "hello,大猫";
		byte[] bytes = str.getBytes();
		String hex = encode(bytes);
		System.out.println(hex);

		byte[] bytes2 = decode(hex);
		System.out.println(new String(bytes2));
		
	}

}

上一篇     下一篇
temporal namespace 信息获取 关键代码及堆栈信息

go.sum 生成

go google uuid 例子

c语言获取UUID

UUID简介

temporal 获取集群信息 关键代码