Java UUID.fromString() 返回无效的 UUID?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18871980/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UUID.fromString() returns an invalid UUID?
提问by kramer65
In my Android app I've got this method which takes a UUID. Unfortunately when I do this:
在我的 Android 应用程序中,我有这个需要 UUID 的方法。不幸的是,当我这样做时:
OverviewEvent overviewevent = eventAdapter.getOverviewEvent(UUID.fromString("0f14d0ab-9605-4a62-a9e4-5ed26688389b"));
I get an error saying java.lang.IllegalArgumentException: Invalid UUID: 100
我收到一条错误消息 java.lang.IllegalArgumentException: Invalid UUID: 100
The implementation of the getOverviewEvent is as follows:
getOverviewEvent 的实现如下:
public OverviewEvent getOverviewEvent(UUID uuid) throws Exception {
// Do stuff
}
Does anybody know how I can solve this?
有谁知道我该如何解决这个问题?
回答by Peter Lawrey
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
回答by cosmozhang
I had this problem too. I think this is because my Java version is low. I found that in my Android application, uuid.split("-")
and uuid.replace("-")
are both useless. I guess it may be caused by that Java regards "-" as a regular expression. However when I try uuid.split("\\-")
and uuid.replace("\\-")
, they didn't work either. I don't know why this happened. I think it's a bug of Java.
我也有这个问题。我想这是因为我的Java版本很低。我发现,在我的Android应用程序,uuid.split("-")
并且uuid.replace("-")
都是无用的。我想这可能是由于Java将“-”视为正则表达式造成的。但是,当我尝试uuid.split("\\-")
and 时uuid.replace("\\-")
,它们也不起作用。我不知道为什么会这样。我认为这是Java的一个错误。
According to Fildor's comment, in Android's implementation, uuid.split("-")
is used to split the uuid string into 5 parts. Then because of the mentioned bug, the uuid string couldn't be spliced into 5 parts. So "Invalid UUID" exception is thrown.
根据Fildor的评论,在 Android 的实现中,uuid.split("-")
用于将 uuid 字符串拆分为 5 部分。然后因为提到的bug,uuid字符串无法拼接成5部分。因此抛出“无效的 UUID”异常。
However, we can modify android's source code to avoid this problem. Using substring()
method, we can split the uuid string into 5 parts. And then we can make the uuid.
但是,我们可以修改android的源代码来避免这个问题。使用substring()
方法,我们可以将 uuid 字符串拆分为 5 部分。然后我们可以制作uuid。
The following codes worked for me:
以下代码对我有用:
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);
}
回答by Recycled Steel
Did you copy and paste the code, I have found that a few characters that look correct are in fact the wrong ACSII code.
您是否复制并粘贴了代码,我发现一些看起来正确的字符实际上是错误的 ACSII 代码。
Remove the - and replace them again.
删除 - 并再次更换它们。
I have had this often with " as different editors/computers may use a slightly different code.
我经常使用“,因为不同的编辑器/计算机可能使用略有不同的代码。