十六进制转码

在 Apache Commons Codec 中提供了 Hex 工具类,该类用来转换十六进制字符串。可以设置用于某些操作的字符集,默认值在DEFAULT_CHARSET_NAME中设置。此类是线程安全的。

字段描述

  • static Charset DEFAULT_CHARSET:默认字符集是 StandardCharsets.UTF_8。

  • static String DEFAULT_CHARSET_NAME:默认字符集名称为 CharEncoding.UTF_8。

构造方法

  • Hex() 使用默认字符集名称 DEFAULT_CHARSET 创建一个新的编解码器

  • Hex(Charset charset) 使用给定的字符集创建一个新的编解码器

  • Hex(String charsetName) 使用给定的字符集名称创建一个新的编解码器

实例

(1)使用 Hex 类的静态方法 encodeHexString() 和 decodeHex() 实现十六进制编码和解码。

import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Hex;
import java.nio.ByteBuffer;

public class HexDemo {

    public static void main(String[] args) {
        byte[] bytes = "Hello World".getBytes();
        String hexStr = "48656c6c6f20576f726c64";
        // 创建字符数组
        char[] chars = new char[hexStr.length()];
        for(int i = 0; i < hexStr.length(); i++) {
            chars[i] = hexStr.charAt(i);
        }

        System.out.println("encodeHexString字节数组:" + Hex.encodeHexString(bytes));
        System.out.println("encodeHexString字节数组:" + Hex.encodeHexString(bytes, false));
        System.out.println("ByteBuffer参数:" + Hex.encodeHexString(ByteBuffer.wrap(bytes)));
        System.out.println("ByteBuffer参数:" + Hex.encodeHexString(ByteBuffer.wrap(bytes), false));

        try {
            // 解析
            System.out.println("decodeHex字符串参数:" + new String(Hex.decodeHex(hexStr)));
            System.out.println("decodeHex字符数组参数:" + new String(Hex.decodeHex(chars)));
        } catch (DecoderException e) {
            e.printStackTrace();
        }
    }

}

输出结果:

encodeHexString字节数组:48656c6c6f20576f726c64
encodeHexString字节数组:48656C6C6F20576F726C64
ByteBuffer参数:48656c6c6f20576f726c64
ByteBuffer参数:48656C6C6F20576F726C64
decodeHex字符串参数:Hello World
decodeHex字符数组参数:Hello World

(2)使用 Hex 的实例方法 encode() 和 decode() 实现十六进制编码和解码。

import org.apache.commons.codec.binary.Hex;
import java.nio.ByteBuffer;

public class HexDemo2 {

    public static void main(String[] args) throws Exception {
        Hex hex = new Hex();
        String str = "hello";
        String hexStr = "68656c6c6f";

        // 将 “hello” 字符串字节数组使用十六进制表示
        byte[] bytes = hex.encode(str.getBytes());
        System.out.println(new String(bytes)); // 68656c6c6f

        // 将 “hello” 字符串字节数组使用十六进制表示
        ByteBuffer byteBuffer = ByteBuffer.allocate(str.getBytes().length);
        byteBuffer.put(str.getBytes());
        byteBuffer.flip();
        byte[] bytes2 = hex.encode(byteBuffer);
        System.out.println(new String(bytes2));

        // 将十六进制字符串解析成字节数组
        byte[] result = hex.decode(hexStr.getBytes());
        System.out.println(new String(result)); // hello

        // 将十六进制字符串解析成字节数组
        ByteBuffer byteBuffer1 = ByteBuffer.allocate(hexStr.getBytes().length);
        byteBuffer1.put(hexStr.getBytes());
        byteBuffer1.flip();
        byte[] result2 = hex.decode(byteBuffer1);
        System.out.println(new String(result2));
    }

}

输出结果:

68656c6c6f
68656c6c6f
hello
hello

(3)利用 Hex 类的 encodeHex() 和 decodeHex() 方法实现十六进制编码和解码。

import org.apache.commons.codec.binary.Hex;

public class HexDemo3 {

    public static void main(String[] args) throws Exception {
        String str = "hello";
        String hexStr = "68656c6c6f";

        // 将 “hello” 字符串字节数组使用十六进制表示
        System.out.println(new String(Hex.encodeHex(str.getBytes())));

        // 将 “hello” 字符串字节数组使用十六进制表示
        System.out.println(new String(Hex.decodeHex(hexStr.toCharArray())));
    }

}

输出结果:

68656c6c6f
hello

查看 Hex 类的详细用法,请参考官网 API 文档,地址如下:

http://commons.apache.org/proper/commons-codec/apidocs/index.html

说说我的看法
全部评论(
没有评论
关于
本网站属于个人的非赢利性网站,转载的文章遵循原作者的版权声明,如果原文没有版权声明,请来信告知:hxstrive@outlook.com
公众号