⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hexcodec.java

📁 《移动Agent技术》一书的所有章节源代码。
💻 JAVA
字号:
/*The HexCodec class contains a few static methods for converting
between byte arrays and hex encoded strings.The complete class is 
shown in the next program.
*/
public class HexCodec {

private static final char[] kDigits = {
	'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};

public static char[] bytesToHex(byte[] raw) {
	int length = raw.length;
	char[] hex = new char[length * 2];
	for (int i = 0; i < length; i++) {
		int value = (raw[i] + 256) % 256;
		int highIndex = value >> 4;
		int lowIndex = value & 0x0f;
		hex[i * 2 + 0] = kDigits[highIndex];
		hex[i * 2 + 1] = kDigits[lowIndex];
	}
	return hex;
}

public static byte[] hexToBytes(char[] hex) {
	int length = hex.length / 2;
	byte[] raw = new byte[length];
	for (int i = 0; i < length; i++) {
		int high = Character.digit(hex[i * 2], 16);
		int low = Character.digit(hex[i * 2 + 1], 16);
		int value = (high << 4) | low;
		if (value > 127) value -= 256;
		raw[i] = (byte)value;
	}
	return raw;
}

public static byte[] hexToBytes(String hex) {
	return hexToBytes(hex.toCharArray());
}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -