PureBasic Forum
http://forums.purebasic.com/english/

Cross-platform UUID generator
http://forums.purebasic.com/english/viewtopic.php?f=12&t=38011
Page 1 of 1

Author:  Mistrel [ Wed Jul 01, 2009 5:44 am ]
Post subject:  Cross-platform UUID generator

According to Wikipedia this conforms to version 4 (random) OSF UUID standard:

Code:
#UUID_FormatType_None=1<<(0)
#UUID_FormatType_Dashes=1<<(1)
#UUID_FormatType_Braces=1<<(2)

Structure UUID
  Byte.b[16]
EndStructure

Procedure.i UniqueID(*UUID.UUID)
  If Not *UUID
    ProcedureReturn 0
  EndIf
 
  For i=0 To 16-1
    *UUID\Byte[i]=Random(255)
  Next
  *UUID\Byte[9]=128+Random(63)
  *UUID\Byte[7]=64+Random(15)
 
  ProcedureReturn *UUID
EndProcedure

Procedure.s FormatUUID(*UUID.UUID, FormatType=#UUID_FormatType_None)
  Protected UUID_String.s
 
  For i=0 To 16-1
    If FormatType&#UUID_FormatType_Dashes
      If i=3 Or i=5 Or i=7
        UUID_String.s+"-"
      EndIf
    EndIf
    UUID_String.s+RSet(Hex(*UUID\Byte[i]&$FF),2,"0")
  Next
 
  If FormatType&#UUID_FormatType_Braces
    UUID_String.s="{"+UUID_String.s+"}"
  EndIf
 
  ProcedureReturn UUID_String.s
EndProcedure

Define UUID.UUID
Debug FormatUUID(UniqueID(@UUID),#UUID_FormatType_Dashes|#UUID_FormatType_Braces)

Author:  Mistrel [ Wed Jul 01, 2009 6:07 am ]
Post subject: 

Here is a smaller version without the version bytes:

Code:
For i=0 To 16-1
  GUID.s+RSet(Hex(Random(255)&$FF),2,"0")
Next

Debug GUID.s

Author:  dell_jockey [ Wed Jul 01, 2009 5:02 pm ]
Post subject: 

Thanks!

Author:  Mistrel [ Wed Jul 01, 2009 9:07 pm ]
Post subject: 

Another method if you want more granular control:

Code:
For i=0 To 32-1
  GUID.s+Chr(Asc(Hex(Random(15))))
Next

Debug GUID.s

Author:  StarBootics [ Wed Jan 23, 2019 3:39 am ]
Post subject:  Re: Cross-platform UUID generator

Hello everyone,

Sorry to re-open an old thread. This is my version of Mistrel's original code. I needed this for my 3D game project.

Code:
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; Project name : Cross-platform UUID generator
; File Name : UUID - Module.pb
; File version: 1.0.0
; Programming : OK
; Programmed by : StarBootics
; Date : 22-01-2019
; Last Update : 22-01-2019
; PureBasic code : V5.70 LTS
; Platform : Windows, Linux, MacOS X
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; Programming notes
;
; Based on Mistrel's original code.
; See here : https://www.purebasic.fr/english/viewtopic.php?f=12&t=38011
;
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

DeclareModule UUID
 
  Declare.s Make(Wrap_With_Brackets.b = #False)

EndDeclareModule

Module UUID
 
  Procedure.s Make(Wrap_With_Brackets.b = #False)

    For Index = 0 To 15
     
      If Index = 7
        Byte.a = 64 + Random(15)
      ElseIf Index = 9
        Byte = 128 + Random(63)
      Else
        Byte = Random(255)
      EndIf
     
      If Index = 4 Or Index = 6 Or Index = 8 Or Index = 10
        UUID_String.s + "-"
      EndIf
     
      UUID_String + RSet(Hex(Byte, #PB_Ascii), 2, "0")
     
    Next
   
    If Wrap_With_Brackets = #True
      UUID_String = "{" + UUID_String + "}"
    EndIf
   
    ProcedureReturn UUID_String
  EndProcedure
 
EndModule

CompilerIf #PB_Compiler_IsMainFile
 
  Macro AddElementEx(ListName, Element)
    AddElement(ListName)
    ListName = Element
  EndMacro
 
  NewList UUID.s()
 
  For index = 0 To 499
    AddElementEx(UUID(), UUID::Make())
  Next
 
  SortList(UUID(), #PB_Sort_Ascending)
 
  ForEach UUID()
    Debug UUID()
  Next
 
CompilerEndIf

; <<<<<<<<<<<<<<<<<<<<<<<
; <<<<< END OF FILE <<<<<
; <<<<<<<<<<<<<<<<<<<<<<<


Best regards
StarBootics

Author:  Kwai chang caine [ Wed Jan 23, 2019 1:13 pm ]
Post subject:  Re: Cross-platform UUID generator

Thanks for sharing 8)

Author:  davido [ Wed Jan 23, 2019 3:06 pm ]
Post subject:  Re: Cross-platform UUID generator

@StarBootics,
I hadn't seen this, so thank you for reincarnating it.

Thank you for sharing. :D

Page 1 of 1 All times are UTC + 1 hour
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/