mirror of
https://gitee.com/kekingcn/file-online-preview.git
synced 2026-03-17 14:43:47 +08:00
388 lines
10 KiB
Java
388 lines
10 KiB
Java
package cn.keking.hutool;
|
||
|
||
import java.awt.*;
|
||
import java.nio.charset.Charset;
|
||
import java.nio.charset.StandardCharsets;
|
||
|
||
/**
|
||
* 十六进制(简写为hex或下标16)在数学中是一种逢16进1的进位制,一般用数字0到9和字母A到F表示(其中:A~F即10~15)。<br>
|
||
* 例如十进制数57,在二进制写作111001,在16进制写作39。<br>
|
||
* 像java,c这样的语言为了区分十六进制和十进制数值,会在十六进制数的前面加上 0x,比如0x20是十进制的32,而不是十进制的20<br>
|
||
* <p>
|
||
* 参考:https://my.oschina.net/xinxingegeya/blog/287476
|
||
*
|
||
* @author Looly
|
||
*/
|
||
public class HexUtil {
|
||
|
||
/**
|
||
* 用于建立十六进制字符的输出的小写字符数组
|
||
*/
|
||
private static final char[] DIGITS_LOWER = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
|
||
/**
|
||
* 用于建立十六进制字符的输出的大写字符数组
|
||
*/
|
||
private static final char[] DIGITS_UPPER = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
|
||
|
||
/**
|
||
* 判断给定字符串是否为16进制数<br>
|
||
* 如果是,需要使用对应数字类型对象的<code>decode</code>方法解码<br>
|
||
* 例如:{@code Integer.decode}方法解码int类型的16进制数字
|
||
*
|
||
* @param value 值
|
||
* @return 是否为16进制
|
||
*/
|
||
public static boolean isHexNumber(String value) {
|
||
final int index = (value.startsWith("-") ? 1 : 0);
|
||
if (value.startsWith("0x", index) || value.startsWith("0X", index) || value.startsWith("#", index)) {
|
||
try {
|
||
Long.decode(value);
|
||
} catch (NumberFormatException e) {
|
||
return false;
|
||
}
|
||
return true;
|
||
} else {
|
||
return false;
|
||
}
|
||
}
|
||
|
||
// ---------------------------------------------------------------------------------------------------- encode
|
||
|
||
/**
|
||
* 将字节数组转换为十六进制字符数组
|
||
*
|
||
* @param data byte[]
|
||
* @return 十六进制char[]
|
||
*/
|
||
public static char[] encodeHex(byte[] data) {
|
||
return encodeHex(data, true);
|
||
}
|
||
|
||
/**
|
||
* 将字节数组转换为十六进制字符数组
|
||
*
|
||
* @param str 字符串
|
||
* @param charset 编码
|
||
* @return 十六进制char[]
|
||
*/
|
||
public static char[] encodeHex(String str, Charset charset) {
|
||
return encodeHex(StrUtil.bytes(str, charset), true);
|
||
}
|
||
|
||
/**
|
||
* 将字节数组转换为十六进制字符数组
|
||
*
|
||
* @param data byte[]
|
||
* @param toLowerCase <code>true</code> 传换成小写格式 , <code>false</code> 传换成大写格式
|
||
* @return 十六进制char[]
|
||
*/
|
||
public static char[] encodeHex(byte[] data, boolean toLowerCase) {
|
||
return encodeHex(data, toLowerCase ? DIGITS_LOWER : DIGITS_UPPER);
|
||
}
|
||
|
||
/**
|
||
* 将字节数组转换为十六进制字符串
|
||
*
|
||
* @param data byte[]
|
||
* @return 十六进制String
|
||
*/
|
||
public static String encodeHexStr(byte[] data) {
|
||
return encodeHexStr(data, true);
|
||
}
|
||
|
||
/**
|
||
* 将字节数组转换为十六进制字符串,结果为小写
|
||
*
|
||
* @param data 被编码的字符串
|
||
* @param charset 编码
|
||
* @return 十六进制String
|
||
*/
|
||
public static String encodeHexStr(String data, Charset charset) {
|
||
return encodeHexStr(StrUtil.bytes(data, charset), true);
|
||
}
|
||
|
||
/**
|
||
* 将字节数组转换为十六进制字符串,结果为小写,默认编码是UTF-8
|
||
*
|
||
* @param data 被编码的字符串
|
||
* @return 十六进制String
|
||
*/
|
||
public static String encodeHexStr(String data) {
|
||
return encodeHexStr(data, StandardCharsets.UTF_8);
|
||
}
|
||
|
||
/**
|
||
* 将字节数组转换为十六进制字符串
|
||
*
|
||
* @param data byte[]
|
||
* @param toLowerCase <code>true</code> 传换成小写格式 , <code>false</code> 传换成大写格式
|
||
* @return 十六进制String
|
||
*/
|
||
public static String encodeHexStr(byte[] data, boolean toLowerCase) {
|
||
return encodeHexStr(data, toLowerCase ? DIGITS_LOWER : DIGITS_UPPER);
|
||
}
|
||
|
||
// ---------------------------------------------------------------------------------------------------- decode
|
||
|
||
/**
|
||
* 将十六进制字符数组转换为字符串,默认编码UTF-8
|
||
*
|
||
* @param hexStr 十六进制String
|
||
* @return 字符串
|
||
*/
|
||
public static String decodeHexStr(String hexStr) {
|
||
return decodeHexStr(hexStr, StandardCharsets.UTF_8);
|
||
}
|
||
|
||
/**
|
||
* 将十六进制字符数组转换为字符串
|
||
*
|
||
* @param hexStr 十六进制String
|
||
* @param charset 编码
|
||
* @return 字符串
|
||
*/
|
||
public static String decodeHexStr(String hexStr, Charset charset) {
|
||
if (StrUtil.isEmpty(hexStr)) {
|
||
return hexStr;
|
||
}
|
||
return decodeHexStr(hexStr.toCharArray(), charset);
|
||
}
|
||
|
||
/**
|
||
* 将十六进制字符数组转换为字符串
|
||
*
|
||
* @param hexData 十六进制char[]
|
||
* @param charset 编码
|
||
* @return 字符串
|
||
*/
|
||
public static String decodeHexStr(char[] hexData, Charset charset) {
|
||
return StrUtil.str(decodeHex(hexData), charset);
|
||
}
|
||
|
||
/**
|
||
* 将十六进制字符数组转换为字节数组
|
||
*
|
||
* @param hexData 十六进制char[]
|
||
* @return byte[]
|
||
* @throws RuntimeException 如果源十六进制字符数组是一个奇怪的长度,将抛出运行时异常
|
||
*/
|
||
public static byte[] decodeHex(char[] hexData) {
|
||
|
||
int len = hexData.length;
|
||
|
||
if ((len & 0x01) != 0) {
|
||
throw new RuntimeException("Odd number of characters.");
|
||
}
|
||
|
||
byte[] out = new byte[len >> 1];
|
||
|
||
// two characters form the hex value.
|
||
for (int i = 0, j = 0; j < len; i++) {
|
||
int f = toDigit(hexData[j], j) << 4;
|
||
j++;
|
||
f = f | toDigit(hexData[j], j);
|
||
j++;
|
||
out[i] = (byte) (f & 0xFF);
|
||
}
|
||
|
||
return out;
|
||
}
|
||
|
||
/**
|
||
* 将十六进制字符串解码为byte[]
|
||
*
|
||
* @param hexStr 十六进制String
|
||
* @return byte[]
|
||
*/
|
||
public static byte[] decodeHex(String hexStr) {
|
||
if (StrUtil.isEmpty(hexStr)) {
|
||
return null;
|
||
}
|
||
return decodeHex(hexStr.toCharArray());
|
||
}
|
||
|
||
// ---------------------------------------------------------------------------------------- Color
|
||
|
||
/**
|
||
* 将{@link Color}编码为Hex形式
|
||
*
|
||
* @param color {@link Color}
|
||
* @return Hex字符串
|
||
* @since 3.0.8
|
||
*/
|
||
public static String encodeColor(Color color) {
|
||
return encodeColor(color, "#");
|
||
}
|
||
|
||
/**
|
||
* 将{@link Color}编码为Hex形式
|
||
*
|
||
* @param color {@link Color}
|
||
* @param prefix 前缀字符串,可以是#、0x等
|
||
* @return Hex字符串
|
||
* @since 3.0.8
|
||
*/
|
||
public static String encodeColor(Color color, String prefix) {
|
||
final StringBuilder builder = new StringBuilder(prefix);
|
||
String colorHex;
|
||
colorHex = Integer.toHexString(color.getRed());
|
||
if (1 == colorHex.length()) {
|
||
builder.append('0');
|
||
}
|
||
builder.append(colorHex);
|
||
colorHex = Integer.toHexString(color.getGreen());
|
||
if (1 == colorHex.length()) {
|
||
builder.append('0');
|
||
}
|
||
builder.append(colorHex);
|
||
colorHex = Integer.toHexString(color.getBlue());
|
||
if (1 == colorHex.length()) {
|
||
builder.append('0');
|
||
}
|
||
builder.append(colorHex);
|
||
return builder.toString();
|
||
}
|
||
|
||
/**
|
||
* 将Hex颜色值转为
|
||
*
|
||
* @param hexColor 16进制颜色值,可以以#开头,也可以用0x开头
|
||
* @return {@link Color}
|
||
* @since 3.0.8
|
||
*/
|
||
public static Color decodeColor(String hexColor) {
|
||
return Color.decode(hexColor);
|
||
}
|
||
|
||
/**
|
||
* 将指定int值转换为Unicode字符串形式,常用于特殊字符(例如汉字)转Unicode形式<br>
|
||
* 转换的字符串如果u后不足4位,则前面用0填充,例如:
|
||
*
|
||
* <pre>
|
||
* '我' =》\u4f60
|
||
* </pre>
|
||
*
|
||
* @param value int值,也可以是char
|
||
* @return Unicode表现形式
|
||
*/
|
||
public static String toUnicodeHex(int value) {
|
||
final StringBuilder builder = new StringBuilder(6);
|
||
|
||
builder.append("\\u");
|
||
String hex = toHex(value);
|
||
int len = hex.length();
|
||
if (len < 4) {
|
||
builder.append("0000", 0, 4 - len);// 不足4位补0
|
||
}
|
||
builder.append(hex);
|
||
|
||
return builder.toString();
|
||
}
|
||
|
||
/**
|
||
* 将指定char值转换为Unicode字符串形式,常用于特殊字符(例如汉字)转Unicode形式<br>
|
||
* 转换的字符串如果u后不足4位,则前面用0填充,例如:
|
||
*
|
||
* <pre>
|
||
* '我' =》\u4f60
|
||
* </pre>
|
||
*
|
||
* @param ch char值
|
||
* @return Unicode表现形式
|
||
* @since 4.0.1
|
||
*/
|
||
public static String toUnicodeHex(char ch) {
|
||
return "\\u" +//
|
||
DIGITS_LOWER[(ch >> 12) & 15] +//
|
||
DIGITS_LOWER[(ch >> 8) & 15] +//
|
||
DIGITS_LOWER[(ch >> 4) & 15] +//
|
||
DIGITS_LOWER[(ch) & 15];
|
||
}
|
||
|
||
/**
|
||
* 转为16进制字符串
|
||
*
|
||
* @param value int值
|
||
* @return 16进制字符串
|
||
* @since 4.4.1
|
||
*/
|
||
public static String toHex(int value) {
|
||
return Integer.toHexString(value);
|
||
}
|
||
|
||
/**
|
||
* 转为16进制字符串
|
||
*
|
||
* @param value int值
|
||
* @return 16进制字符串
|
||
* @since 4.4.1
|
||
*/
|
||
public static String toHex(long value) {
|
||
return Long.toHexString(value);
|
||
}
|
||
|
||
/**
|
||
* 将byte值转为16进制并添加到{@link StringBuilder}中
|
||
*
|
||
* @param builder {@link StringBuilder}
|
||
* @param b byte
|
||
* @param toLowerCase 是否使用小写
|
||
* @since 4.4.1
|
||
*/
|
||
public static void appendHex(StringBuilder builder, byte b, boolean toLowerCase) {
|
||
final char[] toDigits = toLowerCase ? DIGITS_LOWER : DIGITS_UPPER;
|
||
|
||
int high = (b & 0xf0) >>> 4;//高位
|
||
int low = b & 0x0f;//低位
|
||
builder.append(toDigits[high]);
|
||
builder.append(toDigits[low]);
|
||
}
|
||
|
||
// ---------------------------------------------------------------------------------------- Private method start
|
||
|
||
/**
|
||
* 将字节数组转换为十六进制字符串
|
||
*
|
||
* @param data byte[]
|
||
* @param toDigits 用于控制输出的char[]
|
||
* @return 十六进制String
|
||
*/
|
||
private static String encodeHexStr(byte[] data, char[] toDigits) {
|
||
return new String(encodeHex(data, toDigits));
|
||
}
|
||
|
||
/**
|
||
* 将字节数组转换为十六进制字符数组
|
||
*
|
||
* @param data byte[]
|
||
* @param toDigits 用于控制输出的char[]
|
||
* @return 十六进制char[]
|
||
*/
|
||
private static char[] encodeHex(byte[] data, char[] toDigits) {
|
||
final int len = data.length;
|
||
final char[] out = new char[len << 1];//len*2
|
||
// two characters from the hex value.
|
||
for (int i = 0, j = 0; i < len; i++) {
|
||
out[j++] = toDigits[(0xF0 & data[i]) >>> 4];// 高位
|
||
out[j++] = toDigits[0x0F & data[i]];// 低位
|
||
}
|
||
return out;
|
||
}
|
||
|
||
/**
|
||
* 将十六进制字符转换成一个整数
|
||
*
|
||
* @param ch 十六进制char
|
||
* @param index 十六进制字符在字符数组中的位置
|
||
* @return 一个整数
|
||
* @throws RuntimeException 当ch不是一个合法的十六进制字符时,抛出运行时异常
|
||
*/
|
||
private static int toDigit(char ch, int index) {
|
||
int digit = Character.digit(ch, 16);
|
||
if (digit == -1) {
|
||
throw new RuntimeException("Illegal hexadecimal character " + ch + " at index " + index);
|
||
}
|
||
return digit;
|
||
}
|
||
// ---------------------------------------------------------------------------------------- Private method end
|
||
} |