UUID转16字节数组(Java)

最近在写协议的时候,遇到需要将一个36字符长度的UUID转为长度为16的字节数组;

这样处理的话那我们就需要保证唯一性和可还原;

于是我使用了下面的方式:

java 复制代码
     /**
     * uuid转16字节数组
     *
     * @param uuidStr
     * @return
     */
    private static byte[] getBytesByUUID(String uuidStr) {
        UUID uuid = UUID.fromString(uuidStr);
        long mostSignificantBits = uuid.getMostSignificantBits();
        long leastSignificantBits = uuid.getLeastSignificantBits();
        ByteBuffer buffer = ByteBuffer.allocate(16);//开辟16个字节的空间
        buffer.putLong(mostSignificantBits);
        buffer.putLong(leastSignificantBits);
        return buffer.array();
    }

    /**
     * 方式2:原因是uuid的- 对唯一性没有影响
     *
     * @param uuidStr
     * @return
     */
    private static byte[] getBytesByUUIDOther(String uuidStr) {
        String replace = uuidStr.replace("-", "");
        byte[] bytes1 = HexUtils.fromHexString(replace);//16进制转字节数组
        return bytes1;
    }

还原:

java 复制代码
    /**
     * 16位bytes 转uuid
     * @param bytes
     * @return
     */
    public static String getUUIDStrByBytes(byte[] bytes) {
        ByteBuffer buffer = ByteBuffer.wrap(bytes);
        long most = buffer.getLong();
        long least = buffer.getLong();
        UUID uuid = new UUID(most, least);
        return uuid.toString();
    }

测试一下:

java 复制代码
    public static void main(String[] args) throws NoSuchAlgorithmException {

        byte[] bytes = getBytesByUUID("56ad1015-ac93-46dd-8712-9fc4d9ab0171");
        System.out.println("uuid 转 字节数组 " + Arrays.toString(bytes));

        byte[] uuidOther = getBytesByUUIDOther("56ad1015-ac93-46dd-8712-9fc4d9ab0171");
        System.out.println("uuid 转 字节数组 other= " + Arrays.toString(uuidOther));

        String uuidStr = getUUIDStrByBytes(bytes);
        System.out.println("16字节数组转 uuid = " + uuidStr);
    }
    }

结果:

可以还原已经证明了,怎么证明唯一性呢?

作证一

这个问题还有待探讨,写这个文章就是想集思广益,看看这个大模型的回答:

UUID是36位字符,而保证唯一的是除了- 的32字符(uuid是16进制hex 转bytes是16 byte也就是128位),

16进制hex转的bytes因为uuid唯一,所以这个字节数组也唯一;

对于高64位的值mostSigBits和低64位的值leastSigBits也就是唯一的了;(会发现两者的到的数组一样)

作证二

相信大家都知道uuid是唯一的那么,uuid判断唯一的方法一定可以作为依据;

从Java中util下的的UUID的源码来看:

它通过比较uuid的两个属性来确定两个uuid是否相同,

那么我对这两个属性进行转byte数组并组成一个16长度的bytes数组是不是也可以确定它的唯一性呢?(UUID底层也懒得研究)

或者大家有什么更好的方法来实现转换且能证明唯一性呢?

期待大佬们的补充。。。。

相关推荐
发光小北15 分钟前
Profinet 从站转 EtherNet/IP 从站网关如何应用?
网络·网络协议·tcp/ip
Tigshop开源商城22 分钟前
『订单税率+收货地址校验国家字段』功能上新|跨境运营更高效,Tigshop开源商城系统 JAVA v5.8.23 版本更新
java·开源商城系统·tigshop
REDcker33 分钟前
C++变量存储与ELF段布局详解 从const全局到rodata与nm_readelf验证实践
java·c++·面试
wangl_9235 分钟前
Modbus RTU 与 Modbus TCP 深入指南-Wireshark抓包分析实战
网络协议·tcp/ip·wireshark·tcp·modbus·rtu
kobesdu2 小时前
【ROS2实战笔记-19】ROS2 生命周期节点的启动顺序、状态转换陷阱与热备方案
java·前端·笔记·机器人·ros·ros2
neo_Ggx232 小时前
Maven 版本管理详解:SNAPSHOT、Release 与 Nexus 仓库的区别和影响
java·maven
matlabgoodboy2 小时前
软件开发定制小程序APP帮代做java代码代编写C语言设计python编程
java·c语言·小程序
江离w2 小时前
新版vibecoding项目初始化指令
java
源远流长jerry3 小时前
Linux 网络发送机制深度解析:从应用到网线
linux·服务器·网络·网络协议·tcp/ip
tongluowan0073 小时前
Spring MVC 底层工作流程+源码分析
java·spring·mvc