Guid to Base64 in Java

后端 未结 1 785
独厮守ぢ
独厮守ぢ 2021-01-14 18:43

I am converting a Guid to Base64 in C# using the following code:

var id = Guid.Parse(\"be9f1bb6-5c8e-407d-85a3-d5ef31f21b4d\");
var base64=Convert.ToBase64St         


        
相关标签:
1条回答
  • 2021-01-14 19:21

    The structure is a bit different, but swapping some bytes in the first part of the byte array fixes your problem.

    java.util.Base64.Encoder encoder= Base64.getEncoder();
    UUID uuid = UUID.fromString("be9f1bb6-5c8e-407d-85a3-d5ef31f21b4d");
    ByteBuffer bb = ByteBuffer.wrap(new byte[16]);
    bb.putLong(uuid.getMostSignificantBits());
    bb.putLong(uuid.getLeastSignificantBits());
    
    byte[] uuid_bytes = bb.array();
    byte[] guid_bytes = Arrays.copyOf(uuid_bytes,uuid_bytes.length);
    
    guid_bytes[0] = uuid_bytes[3];
    guid_bytes[1] = uuid_bytes[2];
    guid_bytes[2] = uuid_bytes[1];
    guid_bytes[3] = uuid_bytes[0];
    guid_bytes[4] = uuid_bytes[5];
    guid_bytes[5] = uuid_bytes[4];
    guid_bytes[6] = uuid_bytes[7];
    guid_bytes[7] = uuid_bytes[6];
    
    String result = encoder.encodeToString(guid_bytes);
    
    0 讨论(0)
提交回复
热议问题