Android Question GUID vs UUID - Can I use GUID as UUID?

mcqueccu

Well-Known Member
Licensed User
Longtime User
I was calling an API which requires UUID version 4 to set as one of the headers. I decided to use this website ( https://www.uuidgenerator.net/ ) API to generate the UUID which works fine.

Later, I started looking for ways to generate the UUID v4 internally in the app so I came across this thread - https://www.b4x.com/android/forum/threads/b4x-guid.99529/#content

Anyways, I learnt UUID is like a subset of GUID, so I decided to use this B4X GUID code to generate the UUID but when I use it, I get ResponseError. Reason: Bad Request, Response: invalid data was sent in the request.

Even if i change it to lowercase, i still get error

Sub GUID As String
Dim sb As StringBuilder
sb.Initialize
For Each stp As Int In Array(8, 4, 4, 4, 12)
If sb.Length > 0 Then sb.Append("-")
For n = 1 To stp
Dim c As Int = Rnd(0, 16)
If c < 10 Then c = c + 48 Else c = c + 55
sb.Append(Chr(c))
Next
Next
Return sb.ToString
End Sub

But from the same thread when i use colboy's code in post #3, I get a positive response from the API
Sub GetGuid As String
Dim r As Reflector
r.Target = r.RunStaticMethod("java.util.UUID", "randomUUID", Null, Null)
Return r.RunMethod("toString")
End Sub

I need Cross-Platform solution, Does it mean the GUID cannot be used as UUID or is not UUID version 4 compliant?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
B4X:
Sub UUIDv4 As String
   Dim sb As StringBuilder
   sb.Initialize
   For Each stp As Int In Array(8, 4, 4, 4, 12)
       If sb.Length > 0 Then sb.Append("-")
       For n = 1 To stp
           Dim c As Int = Rnd(0, 16)
           If c < 10 Then c = c + 48 Else c = c + 55
           If sb.Length = 19 Then c = Asc("8")
           If sb.Length = 14 Then c = Asc("4")
           sb.Append(Chr(c))
       Next
   Next
   Return sb.ToString.ToLowerCase
End Sub
 
Last edited:
Upvote 0

mcqueccu

Well-Known Member
Licensed User
Longtime User
B4X:
Sub UUIDv4 As String
   Dim sb As StringBuilder
   sb.Initialize
   For Each stp As Int In Array(8, 4, 4, 4, 12)
       If sb.Length > 0 Then sb.Append("-")
       For n = 1 To stp
           Dim c As Int = Rnd(0, 16)
           If c < 10 Then c = c + 48 Else c = c + 55
           If sb.Length = 19 Then c = Asc("8")
           If sb.Length = 14 Then c = Asc("A")
           sb.Append(Chr(c))
       Next
   Next
   Return sb.ToString.ToLowerCase
End Sub


This code is not working for me. I have attached a sample code to test with.
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
B4X:
Sub GUID As String
    Dim stp As String = "00000000-0000-1000-a000-000000000000"
    Dim sb As String = ""
 
    For Index = 0 To stp.Length-1
        If stp.CharAt(Index)="0" Then
            sb=sb & "0123456789ABCDEF".CharAt(Rnd(0, 16))
        Else
            sb=sb & stp.CharAt(Index)
        End If
    Next
    Return sb.ToLowerCase
End Sub
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
Compact version
B4X:
Sub GUID As String
    Dim stp As String = "00000000-0000-1000-a000-000000000000"
    Dim sb As String = ""
 
    For Index = 0 To stp.Length-1
        sb=sb & IIf(stp.CharAt(Index)="0","0123456789ABCDEF".CharAt(Rnd(0, 16)),stp.CharAt(Index))    
    Next
    Return sb.ToLowerCase
End Sub

Ultra compact version
B4X:
Sub GUID As String
    Dim sb As String = ""
    For Each C As Char In Regex.Split("","00000000-0000-1000-a000-000000000000")
        sb=sb & IIf("0"=C,"0123456789ABCDEF".CharAt(Rnd(0, 16)),c)
    Next
    Return sb.ToLowerCase
End Sub
 
Last edited:
Upvote 0
Top