UUID.fromString () returns an invalid UUID?

In my Android app, I have this method that accepts UUIDs. Unfortunately, when I do this:

OverviewEvent overviewevent = eventAdapter.getOverviewEvent(UUID.fromString("0f14d0ab-9605-4a62-a9e4-5ed26688389b"));

I get an error java.lang.IllegalArgumentException: Invalid UUID: 100

The implementation of getOverviewEvent is as follows:

public OverviewEvent getOverviewEvent(UUID uuid) throws Exception {
    // Do stuff
}

Does anyone know how I can solve this?

+4
source share
2 answers

Here is a workaround to avoid using this method,

String s = "0f14d0ab-9605-4a62-a9e4-5ed26688389b";
String s2 = s.replace("-", "");
UUID uuid = new UUID(
        new BigInteger(s2.substring(0, 16), 16).longValue(),
        new BigInteger(s2.substring(16), 16).longValue());
System.out.println(uuid);

prints

0f14d0ab-9605-4a62-a9e4-5ed26688389b
+6
source

. , , Java . , Android uuid.split("-") uuid.replace("-") . , , Java "-" . , uuid.split("\\-") uuid.replace("\\-"), . , . , Java.

Fildor, Android uuid.split("-") uuid 5 . - uuid 5 . "Invalid UUID".

Android, . substring(), uuid 5 . uuid.

:

public static UUID makeUuid(String uuidString) {
    String[] parts = {
            uuidString.substring(0, 7),
            uuidString.substring(9, 12),
            uuidString.substring(14, 17),
            uuidString.substring(19, 22),
            uuidString.substring(24, 35)
    };
    long m1 = Long.parseLong(parts[0], 16);
    long m2 = Long.parseLong(parts[1], 16);
    long m3 = Long.parseLong(parts[2], 16);
    long lsb1 = Long.parseLong(parts[3], 16);
    long lsb2 = Long.parseLong(parts[4], 16);
    long msb = (m1 << 32) | (m2 << 16) | m3;
    long lsb = (lsb1 << 48) | lsb2;
    return new UUID(msb, lsb);
}
0

Source: https://habr.com/ru/post/1502802/


All Articles