UUID.fromString() returns an invalid UUID?

kramer65 picture kramer65 · Sep 18, 2013 · Viewed 32.3k times · Source

In my Android app I've got this method which takes a UUID. Unfortunately when I do this:

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

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

The implementation of the getOverviewEvent is as follows:

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

Does anybody know how I can solve this?

Answer

Peter Lawrey picture Peter Lawrey · Sep 18, 2013

Here is a workaround which avoids 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