Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UUID.fromString() returns an invalid UUID?

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?

like image 280
kramer65 Avatar asked Sep 18 '13 12:09

kramer65


People also ask

What is UUID fromString?

The fromString() method of UUID class in Java is used for the creation of UUID from the standard string representation of the same. Syntax: public static UUID fromString(String UUID_name)

How do I return my UUID?

UUID uuid = UUID. randomUUID(); The third static method returns a UUID object given the string representation of a given UUID: UUID uuid = UUID.

What is UUID in Java?

A class that represents an immutable universally unique identifier (UUID). A UUID represents a 128-bit value. There exist different variants of these global identifiers.


2 Answers

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
like image 147
Peter Lawrey Avatar answered Oct 16 '22 13:10

Peter Lawrey


Did you copy and paste the code, I have found that a few characters that look correct are in fact the wrong ACSII code.

Remove the - and replace them again.

I have had this often with " as different editors/computers may use a slightly different code.

like image 39
Recycled Steel Avatar answered Oct 16 '22 15:10

Recycled Steel