Android Code Snippet [B4X] GUID

Erel

Administrator
Staff member
Licensed User
B4X:
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
Example:
B4X:
Log(GUID)
Log(GUID)
Log(GUID)
Log(GUID)
055AF933-59E8-C059-7291-E3BA80BD9804
60040AA2-B054-3E3B-44D3-80616AD78915
00A21FC9-EFFE-9C4F-9560-ABBECCA47E2E
614B0C4D-C52B-E745-2F3C-B8ED55B67D14

Code to generate UUID version 4: https://www.b4x.com/android/forum/threads/guid-vs-uuid-can-i-use-guid-as-uuid.110970/#post-692302
 
Last edited:

colboy

Member
Licensed User
I found this one some time ago, which seems to work well.

B4X:
Sub GetGuid As String
    Dim r As Reflector
    r.Target = r.RunStaticMethod("java.util.UUID", "randomUUID", Null, Null)
    Return r.RunMethod("toString")
End Sub
 

Erel

Administrator
Staff member
Licensed User
My mistake. It is fixed.

In most cases BytesConverter.HexFromBytes should be used instead of Bit.ToHexString. Therefore it was not implemented in B4i. To keep this snippet simple I used Chr instead.
 

AscySoft

Active Member
Licensed User
Forgive me for asking this, but how should I implement code in post no1 to change my crashlytics build_id stored in "com_crashlytics_build_id.xml" that right now is created "manually" in manifest editor with CreateResource? I think I am missing something so simple!
 

Star-Dust

Expert
Licensed User
B4X:
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
Example:
B4X:
Log(GUID)
Log(GUID)
Log(GUID)
Log(GUID)
055AF933-59E8-C059-7291-E3BA80BD9804
60040AA2-B054-3E3B-44D3-80616AD78915
00A21FC9-EFFE-9C4F-9560-ABBECCA47E2E
614B0C4D-C52B-E745-2F3C-B8ED55B67D14

Code to generate UUID version 4: https://www.b4x.com/android/forum/threads/guid-vs-uuid-can-i-use-guid-as-uuid.110970/#post-692302
My 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
        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
 
Last edited:

AscySoft

Active Member
Licensed User
Please make a new Thread
I did not feel that this is not the topic of discussion. My question sounded rather like...how to use it, what is good for, what it's it pourpouse!
Thank's @Filippo I was aware of your tool. It is a nice that someone was thinking about implementing a GUID change. I do think that only with external tools like yours one coud change "programatically" a GUID (for firebase). When I saw this code I was thinking that somehow @Erel implemented it by (native) code!

Oh, I now realize it is a cross-platform code[b4x], I was thinking at it from a [b4a] perspective
 

Zeev Goldstein

Active Member
Licensed User
B4X:
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
Example:
B4X:
Log(GUID)
Log(GUID)
Log(GUID)
Log(GUID)
055AF933-59E8-C059-7291-E3BA80BD9804
60040AA2-B054-3E3B-44D3-80616AD78915
00A21FC9-EFFE-9C4F-9560-ABBECCA47E2E
614B0C4D-C52B-E745-2F3C-B8ED55B67D14

Code to generate UUID version 4: https://www.b4x.com/android/forum/threads/guid-vs-uuid-can-i-use-guid-as-uuid.110970/#post-692302
THANK YOU SO VERY MUCH !!!
 
Top