All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.gitee.cn9750wang.webtools.utils.HexUtils Maven / Gradle / Ivy

The newest version!
/*
 *    Copyright 2021 wwy
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package com.gitee.cn9750wang.webtools.utils;

import java.nio.charset.StandardCharsets;

/**
 * 16进制编码工具
 *
 * @author anonymous
 */
public class HexUtils {

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

    /**
     * 获取整数对应的16进制编码字符
     * @param index 单个整数
     * @return 16进制字符
     */
    public static char digit(int index){
        return DIGITS[index];
    }

    /**
     * 编码为16进制字符串
     * @param bytes 原始数据
     * @return 16进制字符串
     */
    public static String encodeToString(byte[] bytes) {
        return new String(encode(bytes));
    }

    /**
     * 编码为16进制形式
     * @param data 原始数据
     * @return 16进制数据
     */
    public static char[] encode(byte[] data) {

        int l = data.length;

        char[] out = new char[l << 1];

        // two characters form the hex value.
        for (int i = 0, j = 0; i < l; i++) {
            out[j++] = DIGITS[(0xF0 & data[i]) >>> 4];
            out[j++] = DIGITS[0x0F & data[i]];
        }

        return out;
    }

    /**
     * 解码
     * @param array 16进制数据
     * @return 原始数据
     * @throws IllegalArgumentException 错误的输入数据
     */
    public static byte[] decode(byte[] array) throws IllegalArgumentException {
        return decode(new String(array, StandardCharsets.UTF_8));
    }

    /**
     * 解码
     * @param hex 16进制数据
     * @return 原始数据
     * @throws IllegalArgumentException 错误的输入数据
     */
    public static byte[] decode(String hex) {
        return decode(hex.toCharArray());
    }

    /**
     * 解码
     * @param data 16进制数据
     * @return 原始数据
     * @throws IllegalArgumentException 错误的输入数据
     */
    public static byte[] decode(char[] data) throws IllegalArgumentException {

        int len = data.length;

        int check = len & 0x01;

        if (check != 0) {
            throw new IllegalArgumentException("Odd number of characters.");
        }

        byte[] out = new byte[len >> 1];

        for (int i = 0, j = 0; j < len; i++) {
            int f = toDigit(data[j], j) << 4;
            j++;
            f = f | toDigit(data[j], j);
            j++;
            out[i] = (byte) (f & 0xFF);
        }

        return out;
    }

    private static int toDigit(char ch, int index) throws IllegalArgumentException {
        int digit = Character.digit(ch, 16);
        if (digit == -1) {
            throw new IllegalArgumentException("Illegal hexadecimal charcter " + ch + " at index " + index);
        }
        return digit;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy