I had a difficult time generating a type 1 UUID in NSBasic/CE.  I needed it for 
generation of unique IDs when the device is out of range from the network and 
cannot communicate with a server.

First off, read the wikipedia entry on UUID 
(http://en.wikipedia.org/wiki/Universally_Unique_Identifier).

Also, I found this website to be very helpful in wrapping my head around this:  
http://www.famkruithof.net/guid-uuid-timebased.html

So we have to determine the number of 10 nanoseconds elapsed b/t 10/15/1582 at 
midnight and right now.  Since I am Linux user, I decided to start counting 
seconds from 1/1/1970 at midnight (the timestamp of the last Unix epoch) and 
add a constant value of 12219292800 seconds.

Also, the smallest unit of time that my device will give me is milliseconds, 
which I get from NSBasic.win32.api.Ticks.

Also, I just made up a MAC address.  My device is a Symbol MC70 and I get the 
MAC address by using symbol.WirelessLAN.AdapterMACAddress.  You will have to 
figure out how to get this on your own.

Here is the code:

AddObject "NSBasic.win32.api", "api"
AddObject "newObjects.crypt.Number", "bigNumber"

' multiply seconds by this to get 10 ns (UUID time)
Const CLOCK_MULTIPLIER = "10000000"
' multiply milliseconds by this to get 10 ns (UUID time)
Const TICK_MULTIPLIER = "10000"
' Version number stamped into the UUID to identify it as time-based.
Const VERSION_CLOCK = "1"
' seconds from UUID start date to midnight 01/01/1970: 12,219,292,800
Const UUID_SECONDS_TO_EPOCH = "12219292800"

Function uuidGenerate
   Dim result, timeLow, timeMid, timeHighAndVersion, clockSeqAndReservedValue
   Dim clockSeqLowValue, four, mac1, mac, uuidTime
   uuidTime = uuidTimeIn10NanoSeconds
   mac1 = Split("00:10:01:AB:02:30",":")
   mac = Join(mac1,"")
   timeLow = Right(uuidTime,8) '"xxxxxxxx"
   timeMid = Left(Right(uuidTime,12),4) '"xxxx"
   timeHighAndVersion = VERSION_CLOCK & Left(Right(uuidTime,15),3) '"xxxx"
   clockSeqAndReservedValue = clockSeqAndReserved ' "xx" ' hex version of 
number from 0-255
   clockSeqLowValue = clockSeqLow '"xx" ' Hex version of number from 0-255.  
When number is expressed
   ' as 8 bits to make a byte, it must start with 10xxxxxx.
   four = clockSeqAndReservedValue & clockSeqLowValue
   result = timeLow & "-" & timeMid & "-" & _
      timeHighAndVersion & "-" & four & "-" & mac
   uuidGenerate = result
End Function

Function uuidStartDate
   uuidStartDate = DateSerial(1582,10,15)
End Function

Function epochStartDate
   epochStartDate = DateSerial(1970,01,01)
End Function

Function uuidTimeInSeconds
   uuidTimeInSeconds = 
bigNumber.NewNumber(CStr(DateDiff("s",epochStartDate,Now))).Add(UUID_SECONDS_TO_EPOCH)
End Function

Function partialSecondInMilliseconds
   partialSecondInMilliseconds = Right(CStr(api.Ticks),3)
End Function

Function uuidTimeIn10NanoSeconds
   uuidTimeIn10NanoSeconds = 
bigNumber.NewNumber(CStr(uuidTimeInSeconds)).Mul(CLOCK_MULTIPLIER).Add(bigNumber.NewNumber(partialSecondInMilliseconds).Mul(TICK_MULTIPLIER)).Hex
End Function

Function clockSeqAndReserved
   Dim result
   Randomize
   result = CStr(bigNumber.NewNumber(CStr(Round(Rnd() * 63) + 128)).Hex)
   If (Len(result) = 1) Then
     result = "0" & result
   End If
   clockSeqAndReserved = result
End Function

Function clockSeqLow
   Dim result
   Randomize
   result = CStr(bigNumber.NewNumber(CStr(Round(Rnd() * 255))).Hex)
   If (Len(result) = 1) Then
     result = "0" & result
   End If
   clockSeqLow = result
End Function




--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"nsb-ce" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/nsb-ce?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to