要将字节数组转换为Base64字符串,可以使用Java中的Base64类来实现。以下是一个示例代码:

import java.util.Base64;

public class Main {
    public static void main(String[] args) {
        byte[] byteArray = {1, 2, 3, 4, 5};
        
        // 将字节数组转换为Base64字符串
        String base64String = Base64.getEncoder().encodeToString(byteArray);
        System.out.println(base64String);
        
        // 将Base64字符串转换为字节数组
        byte[] decodedByteArray = Base64.getDecoder().decode(base64String);
        for (byte b : decodedByteArray) {
            System.out.print(b + " ");
        }
    }
}

输出结果:

AQIDBAU=
1 2 3 4 5

在这个示例中,我们先将字节数组 {1, 2, 3, 4, 5} 转换为Base64字符串,然后再将Base64字符串转换回字节数组

byte转为base64

原文地址: https://www.cveoy.top/t/topic/iAQp 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录