Android Code Snippet [B4X] GUID

Discussion in 'Code Snippets' started by Erel, Nov 20, 2018.

  1. Erel

    Erel Administrator Staff Member Licensed User

    Code:
    Sub GUID As String
       
    Dim sb As StringBuilder
       sb.Initialize
       
    For Each stp As Int In Array(844412)
           
    If sb.Length > 0 Then sb.Append("-")
           
    For n = 1 To stp
               
    Dim c As Int = Rnd(016)
               
    If c < 10 Then c = c + 48 Else c = c + 55
               sb.Append(
    Chr(c))
           
    Next
       
    Next
       
    Return sb.ToString
    End Sub
    Example:
    Code:
    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
     
    Last edited: Nov 23, 2018
  2. LucaMs

    LucaMs Expert Licensed User

    Very elegant and compact.
     
    Erel likes this.
  3. colboy

    colboy Member Licensed User

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

    Code:
    Sub GetGuid As String
        
    Dim r As Reflector
        r.Target = r.RunStaticMethod(
    "java.util.UUID""randomUUID"NullNull)
        
    Return r.RunMethod("toString")
    End Sub
     
    Star-Dust likes this.
  4. Alexander Stolte

    Alexander Stolte Well-Known Member Licensed User

    on B4I:
    Bit.ToHexString is a unknown member.
     
  5. DonManfred

    DonManfred Expert Licensed User

    On B4A/B4J and B4I?
    The Code in #1 is Crossplatform.
     
  6. Alexander Stolte

    Alexander Stolte Well-Known Member Licensed User

    On B4A the code works, but in B4I i have a unknown member with this code:
    tohexstring.PNG
     
  7. Erel

    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.
     
    DonManfred and Alexander Stolte like this.
  8. AscySoft

    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!
     
  9. Alexander Stolte

    Alexander Stolte Well-Known Member Licensed User

    Please make a new Thread for this question, thanks.:)
     
    Erel likes this.
  10. Star-Dust

    Star-Dust Expert Licensed User

    I thought of a variant of your code

    Code:
    Sub GUID As String
        
    Dim stp As String = "00000000-0000-0000-000000000000"
        
    Dim Hex As String = "0123456789ABCDEF"
        
    Dim sb As String = ""
     
        
    For Index = 0 To stp.Length-1
                
    If stp.CharAt(Index)="0" Then
                    sb=sb & Hex.CharAt(
    Rnd(016))
                
    Else
                    sb=sb & 
    "-"
                
    End If
        
    Next
        
    Return sb.ToString
    End Sub
     
    Last edited: Nov 28, 2018
    advansis and Filippo like this.
  11. Filippo

    Filippo Expert Licensed User

  12. AscySoft

    AscySoft Active Member Licensed User

    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
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice