Java 字节数组(byte[])与字符串(16进制/Base64)的相互转换

本文链接: https://blog.csdn.net/xietansheng/article/details/88421655

MD5、SHA-1 等 Hash 值的计算结果通常转换为 16 进制字符串的形式保存。

RSA 等算法的密钥通常转换为 Base64 字符串保存。图片也可以编码为 Base64 字符串直接以文本的形式放到 HTML 中显示图片。

1. byte[] <-> 16进制字符串

1.1 封装工具类:HexUtils

HexUtils.java工具类完整源码:

package com.xiets.data;

/**
 * 16进制字符串 与 byte数组 相互转换工具类
 */
public class HexUtils {

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

    /**
     * byte数组 转换成 16进制小写字符串
     */
    public static String bytes2Hex(byte[] bytes) {
        if (bytes == null || bytes.length == 0) {
            return null;
        }

        StringBuilder hex = new StringBuilder();

        for (byte b : bytes) {
            hex.append(HEXES[(b >> 4) & 0x0F]);
            hex.append(HEXES[b & 0x0F]);
        }

        return hex.toString();
    }

    /**
     * 16进制字符串 转换为对应的 byte数组
     */
    public static byte[] hex2Bytes(String hex) {
        if (hex == null || hex.length() == 0) {
            return null;
        }

        char[] hexChars = hex.toCharArray();
        byte[] bytes = new byte[hexChars.length / 2];   // 如果 hex 中的字符不是偶数个, 则忽略最后一个

        for (int i = 0; i < bytes.length; i++) {
            bytes[i] = (byte) Integer.parseInt("" + hexChars[i * 2] + hexChars[i * 2 + 1], 16);
        }

        return bytes;
    }

}

HexUtils类中的两个公开静态方法:

// byte数组 转换成 16进制小写字符串
static String bytes2Hex(byte[] bytes)

// 16进制字符串 转换为对应的 byte数组
static byte[] hex2Bytes(String hex)

1.2 HexUtils 工具类的使用

package com.xiets.data;

/**
 * @author xietansheng
 */
public class Main {

    public static void main(String[] args) {
        String data = "Hello World";

        // byte[] 转换为 16进制字符串
        String hex = HexUtils.bytes2Hex(data.getBytes());
        System.out.println(hex);                    // 输出: 48656c6c6f20576f726c64

        // 16进制字符串 转换为 byte[]
        byte[] bytes = HexUtils.hex2Bytes(hex);
        System.out.println(new String(bytes));      // 输出: Hello World
    }

}

2. byte[] <-> Base64 字符串

2.1 封装工具类:Base64Utils

Base64Utils.java工具类完整源码:

package com.xiets.data;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * Base64 转换工具
 */
public class Base64Utils {

    /**
     * byte数组 转换为 Base64字符串
     */
    public static String encode(byte[] data) {
        return new BASE64Encoder().encode(data);
    }

    /**
     * Base64字符串 转换为 byte数组
     */
    public static byte[] decode(String base64) {
        try {
            return new BASE64Decoder().decodeBuffer(base64);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new byte[0];
    }

    /**
     * 把文件内容编码为 Base64字符串, 只能编码小文件(例如文本、图片等)
     */
    public static String encodeFile(File file) throws Exception {
        InputStream in = null;
        ByteArrayOutputStream bytesOut = null;

        try {
            in = new FileInputStream(file);
            bytesOut = new ByteArrayOutputStream((int) file.length());

            byte[] buf = new byte[1024];
            int len = -1;

            while ((len = in.read(buf)) != -1) {
                bytesOut.write(buf, 0, len);
            }
            bytesOut.flush();

            return encode(bytesOut.toByteArray());

        } finally {
            close(in);
            close(bytesOut);
        }
    }

    /**
     * 把 Base64字符串 转换为 byte数组, 保存到指定文件
     */
    public static void decodeFile(String base64, File file) throws Exception {
        OutputStream fileOut = null;
        try {
            fileOut = new FileOutputStream(file);
            fileOut.write(decode(base64));
            fileOut.flush();
        } finally {
            close(fileOut);
        }
    }

    private static void close(Closeable c) {
        if (c != null) {
            try {
                c.close();
            } catch (IOException e) {
                // nothing
            }
        }
    }

}

Base64Utils工具类中的几个公开静态方法:

// byte数组 转换为 Base64字符串
static String encode(byte[] data)
// Base64字符串 转换为 byte数组
static byte[] decode(String base64)

// 把文件内容编码为 Base64字符串, 只能编码小文件(例如文本、图片等)
static String encodeFile(File file)
// 把 Base64字符串 转换为 byte数组, 保存到指定文件
static void decodeFile(String base64, File file)

2.2 Base64Utils 工具类的使用

package com.xiets.data;

import java.io.File;

/**
 * @author xietansheng
 */
public class Main {

    public static void main(String[] args) throws Exception {
        String data = "Hello World";

        // 编码: byte[] 转换为 Base64字符串
        String base64 = Base64Utils.encode(data.getBytes());
        System.out.println(base64);                 // 输出: SGVsbG8gV29ybGQ

        // 解码: Base64字符串 转换为 byte[]
        byte[] bytes = Base64Utils.decode(base64);
        System.out.println(new String(bytes));      // 输出: Hello World

        /*
         * 对文件进行 Base64 编码
         */
        // 编码: 把文件内容编码为 Base64字符串
        String fileBase64Str = Base64Utils.encodeFile(new File("demo.png"));
        System.out.println(fileBase64Str);

        // Base64 字符串格式的图片在 html img 标签中的直接显示格式: 
        // <img src="https://img-blog.csdnimg.cn/2022010703452067536.png"/>

        // 解码: 把 Base64字符串 转换为 byte数组, 保存到指定文件
        Base64Utils.decodeFile(fileBase64Str, new File("demo2.png"));

        // 对比 demo.png 和 demo2.png 两个文件的 MD5 将会完全相同
    }

}

  • 12
    点赞
  • 42
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

谢TS

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值