Information générale
Catégorie:
Codage, syntaxe et commandes
Titre:
Unique ID Generation
I don't remeber where I found the below code, but thanks to the original author. I have a coupla questions on it, though.
1. What is this Windows library based on (NIC, hd id, etc)?
2. How do I know these are "really" unique across time & space?
*Create a Universally Unique ID
*Adapted from the book Visual FoxPro 5
*Enterprise Development One of the most
*common problems faced with developers
*is a method of creating unique keys. This
*is where a UUID (Univerally Unique ID)
*comes in. UUID's are a IEEE standard
*method of generating Universally Unique
*values. What this means is the values
*returned are unique across time and
*space. The code below calls the Windows API
*and returns a UUID.
Function getUUID
*-- The UUID Create function is in this library
DECLARE Integer UuidCreate IN C:\Windows\System\RPCRT4.DLL ;
String @ UUID && note that mixed case is important for UuidCreate
*-- Create a buffer
lcUUID = Space(16)
*-- generate the key
=UUIDCreate(@lcUUID)
*-- Convert the binaries to hex values
lcPart1 = BinAsHex(Substr(lcUUID,1,4))
lcPart2 = BinAsHex(Substr(lcUUID,5,4))
lcPart3 = BinAsHex(Substr(lcUUID,8,4))
lcPart4 = BinAsHex(Substr(lcUUID,13,4))
Local lcRetVal
lcRetVal = lcPart1 + lcPart2 + lcPart3 + lcPart4
Return Left(lcRetVal,8) + "-" + substr(lcRetVal,9,4) + "-" + substr(lcRetVal,13,4) + "-" + ;
substr(lcRetVal,17,4) + "-" + Right(lcRetVal,12)
PROCEDURE binashex
Parameters lcBin
* translates a binary string into character string of hex
LOCAL lcChars, lnBin
lcChars = ""
FOR lnDigit = 1 TO LEN(lcBin)
lnBin = ASC(SUBSTR(lcBin, lnDigit, 1))
lcChars = lcChars + Hex2Char(INT(lnBin/16)) + ;
Hex2Char(MOD(lnBin,16))
ENDFOR
RETURN(lcChars)
PROCEDURE hex2char
* takes a hex digit value and returns a character from 0-9 & A-F
LPARAMETERS lnHex
DO CASE
CASE BETWEEN(lnHex,0,9)
lnAsc = 48 + lnHex
CASE BETWEEN(lnHex,10,15)
lnAsc = 65 + lnHex - 10
ENDCASE
RETURN(CHR(lnAsc))
Suivant
Répondre
Voir le fil de ce thread
Voir le fil de ce thread à partir de ce message seulement
Voir tous les messages de ce thread
Voir tous les messages de ce thread à partir de ce message seulement