简体   繁体   English

如何在java中将Base64编码的字符串转换为UUID

[英]How to convert Base64 encoded string to UUID in java

this is the encoded string这是编码的字符串

YjRmYTJhMGEtYjI0ZC00ZjU4LTg2ZDktNTNiN2I2ODM4YjY3IzU1YjFjNGUzZTRiMGQ4OTUxMGM2YWEyNw

i want to generate UUID for this我想为此生成 UUID

You can convert as below using 2 functions.您可以使用 2 个函数进行如下转换。 apache commons codec jar has some methods to encode and decode UUID using Base64 . apache commons codec jar有一些使用Base64编码和解码UUID方法。

Link to download apache commons codec jar: http://www.java2s.com/Code/JarDownload/apache-commons/apache-commons-codec-1.4.jar.zip apache commons codec jar下载链接: http : //www.java2s.com/Code/JarDownload/apache-commons/apache-commons-codec-1.4.jar.zip

import java.nio.ByteBuffer;
import java.util.UUID;

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


public class Solution1 {
    public static void main(String[] args) {
        String uuid_str = "YjRmYTJhMGEtYjI0ZC00ZjU4LTg2ZDktNTNiN2I2ODM4YjY3IzU1YjFjNGUzZTRiMGQ4OTUxMGM2YWEyNw";
        String uuid_as_64 = uuidFromBase64(uuid_str);
        System.out.println("as base64: "+uuid_as_64);
        System.out.println("as uuid: "+uuidFromBase64(uuid_as_64));
    }

    private static String uuidToBase64(String str) {
        Base64 base64 = new Base64();
        UUID uuid = UUID.fromString(str);
        ByteBuffer bb = ByteBuffer.wrap(new byte[16]);
        bb.putLong(uuid.getMostSignificantBits());
        bb.putLong(uuid.getLeastSignificantBits());
        return base64.encodeBase64URLSafeString(bb.array());
    }
    private static String uuidFromBase64(String str) {
        Base64 base64 = new Base64(); 
        byte[] bytes = base64.decodeBase64(str);
        ByteBuffer bb = ByteBuffer.wrap(bytes);
        UUID uuid = new UUID(bb.getLong(), bb.getLong());
        return uuid.toString();
    }
}

Output:输出:

as base64: 62346661-3261-3061-2d62-3234642d3466作为 base64:62346661-3261-3061-2d62-3234642d3466

as uuid: eb6df8eb-aeb5-fb7d-bad7-edf4eb5fb677作为 uuid:eb6df8eb-aeb5-fb7d-bad7-edf4eb5fb677

For more, you can follow the tutorial:有关更多信息,您可以按照教程进行操作:

  1. http://www.baeldung.com/java-base64-encode-and-decode http://www.baeldung.com/java-base64-encode-and-decode
  2. http://www.tutorialspoint.com/java8/java8_base64.htm http://www.tutorialspoint.com/java8/java8_base64.htm
  3. How can I convert a UUID to base64? 如何将 UUID 转换为 base64?
  4. Storing UUID as base64 String 将 UUID 存储为 base64 字符串

The above base64 string decodes to ASCII string "b4fa2a0a-b24d-4f58-86d9-53b7b6838b67#55b1c4e3e4b0d89510c6aa27", so:上面的base64字符串解码为ASCII字符串“b4fa2a0a-b24d-4f58-86d9-53b7b6838b67#55b1c4e3e4b0d89510c6aa27”,所以:

import org.apache.commons.codec.binary.Base64;
public class Solution {
    private static String uuidFromBase64(String str) {
        Base64 base64 = new Base64(); 
        byte[] bytes = base64.decodeBase64(str);
        String s = new String(bytes);
        String trimmed = s.split("#")[0];
        return trimmed;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM