B4J Question [B4X] How to get a GUIDType Simple Type / GUID

imbault

Well-Known Member
Licensed User
Hi to all,

I'm tryig to get this pattern :
The GUIDType simple type is a string that is restricted by the following pattern:

  • \{[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}\}
I tried this code, which gives an error : java.lang.RuntimeException: Method: UUID not found in: java.util.UUID
B4X:
   Dim jo As JavaObject
   Return jo.InitializeStatic("java.util.UUID").RunMethod("UUID", Null)
Any clue, please
 

Erel

Administrator
Staff member
Licensed User
Cross platform solution:
B4X:
Sub udid As String
   Dim sr As SecureRandom
   Dim data(16) As Byte
   sr.GetRandomBytes(data)
   Dim bc As ByteConverter
   Dim hex As String = bc.HexFromBytes(data)
   Dim sb As StringBuilder
   sb.Initialize
   Dim i As Int
   For Each stp As Int In Array(8, 4, 4, 4, 12)
       If sb.Length > 0 Then sb.Append("-")
       sb.Append(hex.SubString2(i, i + stp))
       i = i + stp
   Next
   Return sb.ToString
End Sub
Dependencies:
B4A / B4J - ByteConverter, Encryption
B4i - iEncryption, iRandomAccessFile
 
Top