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

Lucas Siqueira

Active 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

suggestion

The previous code fixed the character 8 in the first position of the fourth group of characters. Now it can be 8, 9, a, or b

B4X:
Sub UUIDv4 As String
    ' Initializes a StringBuilder to construct the UUID string
    Dim sb As StringBuilder
    sb.Initialize

    ' Defines the format of the UUID with 5 groups (8, 4, 4, 4, 12 characters respectively)
    For Each stp As Int In Array(8, 4, 4, 4, 12)
        ' Adds a hyphen (-) between groups, except before the first group
        If sb.Length > 0 Then sb.Append("-")

        ' Iterates through the number of characters in each group
        For n = 1 To stp
            ' Generates a random number between 0 and 15 (hexadecimal)
            Dim c As Int = Rnd(0, 16)

            ' Converts the random number into a hexadecimal character (0-9 or a-f)
            If c < 10 Then
                c = c + 48 ' Converts numbers 0-9 to their ASCII values (48 = '0')
            Else
                c = c + 87 ' Converts numbers 10-15 to their ASCII values ('a'-'f', 87 is the offset)
            End If

            ' If we're at the start of the fourth group (sb.Length = 19), adjusts the first character
            ' This value must be 8, 9, a, or b, as defined by the UUID v4 specification
            If sb.Length = 19 Then
                ' Creates an array containing the possible values for the first character of the fourth group
                Dim variantArray() As String = Array As String("8", "9", "a", "b")
                ' Randomly selects one value from the array and converts it to ASCII
                c = Asc(variantArray(Rnd(0, variantArray.Length)))
            End If

            ' If we're at the start of the third group (sb.Length = 14), defines the UUID version as 4
            ' This is required by the UUID v4 specification
            If sb.Length = 14 Then
                c = Asc("4") ' Sets the character to "4", indicating version 4
            End If

            ' Appends the generated character to the StringBuilder
            sb.Append(Chr(c))
        Next
    Next

    ' Returns the complete UUID string, converted to lowercase
    Return sb.ToString.ToLowerCase
End Sub
 
Upvote 0
Top