• UUID转16字节数组(Java)


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

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

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

         /**
         * 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;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    还原:

        /**
         * 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();
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    测试一下:

        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);
        }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    结果:
    在这里插入图片描述

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

    作证一
    这个问题还有待探讨,写这个文章就是想集思广益,看看这个大模型的回答:
    在这里插入图片描述
    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底层也懒得研究)

    或者大家有什么更好的方法来实现转换且能证明唯一性呢?
    期待大佬们的补充。。。。

  • 相关阅读:
    代码随想录 单调栈part2
    基于matlab的异步(感应)电机直接转矩控制系统
    ROS2系列知识(3):环境配置
    Java学习笔记——序列化
    修炼k8s+flink+hdfs+dlink(四:k8s(二)架构)
    [Java]java类的加载:初始化
    I Pa?sWorD(2023icpc网络预选赛)
    如何优化你的Vue.js应用以获得最佳性能
    windows安装mysql时卡write configuration file曲线救国 mysql 5.7.39 免安装(ZIP压缩包)版本安装配置
    justjavac:从辍学到成为Deno核心代码贡献者,我的十年编程生涯
  • 原文地址:https://blog.csdn.net/an_gentle_killer/article/details/134019347