Jump to content

UID


erifash
 Share

Recommended Posts

Thanks to datkewlguy and all the people that worked with him to create the Scramble functions.

These functions can produce and check a unique number that can be used to identify things. It uses a system of random numbers and thier sum converted into hex and scrambled to create a UID.

Here's the code:

Func _UIDCreate()
    Local $n_1 = Int(Random(1, 255)), $n_2 = Int(Random(1, 255)), $n_3 = Int(Random(0, 255)), $n_4 = Int(Random(0, 255)), $n_5 = Hex($n_1 + $n_2 + $n_3 + $n_4, 8), $n_1 = Hex($n_1, 2), $n_2 = Hex($n_2, 2), $n_3 = Hex($n_3, 2), $n_4 = Hex($n_4, 2)
    Return _Scramble($n_1 & $n_2 & $n_3 & $n_4 & "-" & $n_5)
EndFunc

Func _UIDCheck($sz_UID)
    If $sz_UID = "0000000000000000-" Then Return 0
    Local $sz_decUID = _UnScramble($sz_UID), $sz_tsplit = StringSplit($sz_decUID, "-"), $sz_part1 = StringSplit($sz_tsplit[1], ""), $n_dec1 = Dec($sz_part1[1] & $sz_part1[2]), $n_dec2 = Dec($sz_part1[3] & $sz_part1[4]), $n_dec3 = Dec($sz_part1[5] & $sz_part1[6]), $n_dec4 = Dec($sz_part1[7] & $sz_part1[8])
    If $n_dec1 + $n_dec2 + $n_dec3 + $n_dec4 = Dec($sz_tsplit[2]) Then Return 1
    Return 0
EndFunc

I don't really know what this can be used for I just thought it would be a cool idea, but I do hope it can be of help to someone. And, as always , questions/comments are greatly appreciated! :(

Link to comment
Share on other sites

Here's a ready made one from Microsoft. Latest beta needed for DllStruct support of course.

Opt("MustDeclareVars", 1)

ConsoleWrite(_CreateGUID() & @LF)

Func _CreateGUID()
    Local $GUIDSTRUCT
    Local $aDllRet
    Local $sGUID
    
    $GUIDSTRUCT = DllStructCreate("int;int;int;byte[8]")
    If Not @error Then
        $aDllRet = DllCall("ole32.dll", "long", "CoCreateGuid", "ptr", DllStructGetPtr($GUIDSTRUCT))
        If Not @error And $aDllRet[0] = 0 Then
            $aDllRet = DllCall("ole32.dll", "long", "StringFromGUID2", _ 
                "ptr", DllStructGetPtr($GUIDSTRUCT), _ 
                "wstr", "", _ 
                "int", 40)
            If Not @error And $aDllRet[0] > 0 Then $sGUID = $aDllRet[2]
        EndIf
        DllStructDelete($GUIDSTRUCT)    
    EndIf
    Return $sGUID
EndFunc
Link to comment
Share on other sites

Does that Dll one produce a unique ID? If not, then this function I just whipped up is even faster. :(

Func _MyGUID()
    Local $a_GUID[8]
    For $i = 0 To 7
        $a_GUID[$i] = Hex(Random(0, 0xFFFF), 4)
    Next
    Return '{' & $a_GUID[0] & $a_GUID[1] & '-' &_
           $a_GUID[2] & '-' & $a_GUID[3] & '-' &_
           $a_GUID[4] & '-' & $a_GUID[5] &_
           $a_GUID[6] & $a_GUID[7] & '}'
EndFunc

This one runs in about .60 seconds, the MS one seems to average up around .68. Plus mine is less code :(

Link to comment
Share on other sites

Does that Dll one produce a unique ID?

Yah, that's the trick. Sez Microsoft:

A GUID is a 128-bit integer (16 bytes) that can be used across all computers and networks wherever a unique identifier is required. Such an identifier has a very low probability of being duplicated.

Link

BlueBearrOddly enough, this is what I do for fun.
Link to comment
Share on other sites

Does that Dll one produce a unique ID? If not, then this function I just whipped up is even faster. :(

Func _MyGUID()
    Local $a_GUID[8]
    For $i = 0 To 7
        $a_GUID[$i] = Hex(Random(0, 0xFFFF), 4)
    Next
    Return '{' & $a_GUID[0] & $a_GUID[1] & '-' &_
           $a_GUID[2] & '-' & $a_GUID[3] & '-' &_
           $a_GUID[4] & '-' & $a_GUID[5] &_
           $a_GUID[6] & $a_GUID[7] & '}'
EndFunc

This one runs in about .60 seconds, the MS one seems to average up around .68. Plus mine is less code :(

<{POST_SNAPBACK}>

Not sure what algo MS uses to generate GUIDs but I believe it's slightly more sophisticated than your random method.

Have a look here for a bit more info on the actual function that generates the GUIDs.

To quote from the Remarks section:

Computers with ethernet/token ring addresses generate UUIDs that are guaranteed to be globally unique

Link to comment
Share on other sites

Does that Dll one produce a unique ID? If not, then this function I just whipped up is even faster. :(

Func _MyGUID()
    Local $a_GUID[8]
    For $i = 0 To 7
        $a_GUID[$i] = Hex(Random(0, 0xFFFF), 4)
    Next
    Return '{' & $a_GUID[0] & $a_GUID[1] & '-' &_
           $a_GUID[2] & '-' & $a_GUID[3] & '-' &_
           $a_GUID[4] & '-' & $a_GUID[5] &_
           $a_GUID[6] & $a_GUID[7] & '}'
EndFunc

This one runs in about .60 seconds, the MS one seems to average up around .68. Plus mine is less code :(

<{POST_SNAPBACK}>

Yes, but how would you check it to make sure it is valid? Mine is able to be checked because it just adds up the decimal values and converts to hex.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...