It is currently Sun Dec 15, 2019 10:46 am

All times are UTC + 1 hour




Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: Cross-platform UUID generator
PostPosted: Wed Jul 01, 2009 5:44 am 
Offline
Addict
Addict

Joined: Sat Jun 30, 2007 8:04 pm
Posts: 3362
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)


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Wed Jul 01, 2009 6:07 am 
Offline
Addict
Addict

Joined: Sat Jun 30, 2007 8:04 pm
Posts: 3362
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


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Wed Jul 01, 2009 5:02 pm 
Offline
Enthusiast
Enthusiast

Joined: Sat Jan 24, 2004 6:56 pm
Posts: 768
Thanks!

_________________
cheers,
dell_jockey
________
http://blog.forex-trading-ideas.com


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Wed Jul 01, 2009 9:07 pm 
Offline
Addict
Addict

Joined: Sat Jun 30, 2007 8:04 pm
Posts: 3362
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


Top
 Profile  
Reply with quote  
 Post subject: Re: Cross-platform UUID generator
PostPosted: Wed Jan 23, 2019 3:39 am 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Sun Jul 07, 2013 11:35 am
Posts: 450
Location: Canada
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

_________________
The Stone Age did not end due to a shortage of stones !


Top
 Profile  
Reply with quote  
 Post subject: Re: Cross-platform UUID generator
PostPosted: Wed Jan 23, 2019 1:13 pm 
Offline
Addict
Addict
User avatar

Joined: Sun Nov 05, 2006 11:42 pm
Posts: 4548
Location: Lyon - France
Thanks for sharing 8)

_________________
ImageThe happiness is a road...
Not a destination


Top
 Profile  
Reply with quote  
 Post subject: Re: Cross-platform UUID generator
PostPosted: Wed Jan 23, 2019 3:06 pm 
Offline
Addict
Addict

Joined: Fri Nov 09, 2012 11:04 pm
Posts: 1715
Location: Uttoxeter, UK
@StarBootics,
I hadn't seen this, so thank you for reincarnating it.

Thank you for sharing. :D

_________________
DE AA EB


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye