package com.util; /** * @说明 十六进制与十进制之间的转换 * @author 崔素强 * @version 1.0 * @since */ public class ByteAndStr16 { private final static byte[] hex = "0123456789ABCDEF".getBytes(); private static int parse(char c) { if (c >= 'a') return (c - 'a' + 10) & 0x0f; if (c >= 'A') return (c - 'A' + 10) & 0x0f; return (c - '0') & 0x0f; } // 从字节数组到十六进制字符串转换 public static String Bytes2HexString(byte[] b) { byte[] buff = new byte[3 * b.length]; for (int i = 0; i < b.length; i++) { buff[3 * i] = hex[(b[i] >> 4) & 0x0f]; buff[3 * i + 1] = hex[b[i] & 0x0f]; buff[3 * i + 2] = 45; } String re = new String(buff); return re.replace("-", " "); } // 从十六进制字符串到字节数组转换 public static byte[] HexString2Bytes(String hexstr) { hexstr = hexstr.replace(" ", ""); byte[] b = new byte[hexstr.length() / 2]; int j = 0; for (int i = 0; i < b.length; i++) { char c0 = hexstr.charAt(j++); char c1 = hexstr.charAt(j++); b[i] = (byte) ((parse(c0) << 4) | parse(c1)); } return b; } }
英文名称:hexadecimal number system,是计算机中数据的一种表示方法。
同我们日常中就表示法不一样。它由0-9,A-F组成,字母不区分大小写。与10进制的对应关系是:0-9对应0-9;A-F对应10-15;N进制的数可以用0---(N-1)的数表示,超过9的用字母A-F。
Java小强
未曾清贫难成人,不经打击老天真。
自古英雄出炼狱,从来富贵入凡尘。
发表评论: