Mega Code Archive

 
Categories / Delphi / String
 

TGUID String

Title: TGUID String Question: Using a GUID filled in by the user Answer: It is not possible to fill a variabele of the type TGUID with a string, unless this is a constant e.g. const appguid: tguid = '{238C0E8D-5465-40C3-9FF1-8EC358AE6BDB}'; In order to convert a GUID(CLSID) to a string, I am using the functions listed below. To use these functions, call function stringtoguid(GUID) where GUID is a valid GUID. (syntax: '{xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx}'). The return value is the GUID as a TGUID. ------------------------------------------------------------------------------- function expnumber(number, exponent: integer): integer; var counter: integer; begin result:=1; if exponent=0 then exit; for counter:=1 to exponent do begin result:=result*number; end; end; function hextodec(hex: string): integer; var counter: integer; value: integer; begin result:=0; for counter:=0 to length(hex)-1 do begin case hex[length(hex)-counter] of 'A': value:=10; 'B': value:=11; 'C': value:=12; 'D': value:=13; 'E': value:=14; 'F': value:=15; else value:=strtoint(hex[length(hex)-counter]); end; result:=result+value*expnumber(16, counter); end; end; function stringtoguid(GUID: string): TGUID; var counter: integer; newword: string; begin //Expect GUID '{xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx}' as string if not, exit //result: '{D1 - D2 - D3 - D4a[0..1] - D4b[2..7]}' if (guid[1] '{') or (guid[10] '-') or (guid[15] '-') or (guid[20] '-') or (guid[25] '-') or (guid[38] '}') then exit; //Do D1 newword:=''; for counter:=2 to 9 do newword:=newword+guid[counter]; Result.d1:=hextodec(newword); //Do D2 newword:=''; for counter:=11 to 14 do newword:=newword+guid[counter]; Result.d2:=hextodec(newword); //Do D3 newword:=''; for counter:=16 to 19 do newword:=newword+guid[counter]; Result.d3:=hextodec(newword); //Do D4a Result.D4[0]:=hextodec(guid[21]+guid[22]); Result.D4[1]:=hextodec(guid[23]+guid[24]); //Do D4b Result.D4[2]:=hextodec(guid[26]+guid[27]); Result.D4[3]:=hextodec(guid[28]+guid[29]); Result.D4[4]:=hextodec(guid[30]+guid[31]); Result.D4[5]:=hextodec(guid[32]+guid[33]); Result.D4[6]:=hextodec(guid[34]+guid[35]); Result.D4[7]:=hextodec(guid[36]+guid[37]); end; ------------------------------------------------------------------------------- This can also be the other way around, GUID to string. For this the following functions. Use guidtostr(GUID) function with a valid TGUID variabele, and the retrun value will be the GUID in a string. ------------------------------------------------------------------------------- function dectohex(decValue: integer): string; var counter, counter2, times: integer; value: integer; begin value:=decvalue; result:=''; counter:=0; repeat counter2:=value-expnumber(16, counter); inc(counter); until (value-expnumber(16, counter) counter2); counter:=counter-1; times:=value div expnumber(16, counter); value:=value-times*expnumber(16, counter); case times of 10: result:='A'; 11: result:='B'; 12: result:='C'; 13: result:='D'; 14: result:='E'; 15: result:='F'; else result:=inttostr(times); end; for counter2:=1 to counter do begin times:=value div expnumber(16, counter-counter2); value:=value-times*expnumber(16, counter-counter2); case times of 10: result:=result+'A'; 11: result:=result+'B'; 12: result:=result+'C'; 13: result:=result+'D'; 14: result:=result+'E'; 15: result:=result+'F'; else result:=result+inttostr(times); end; end; end; function guidtostring(GUID: TGUID): string; var counter: integer; begin //Input GUID '{xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx}' as TGUID //result: '{D1 - D2 - D3 - D4a[0..1] - D4b[2..7]}' result:='{'+dectohex(GUID.D1)+'-'+dectohex(GUID.D2)+'-'+dectohex(GUID.D3)+'-'+dectohex(GUID.D4[0])+dectohex(GUID.D4[1])+'-'; for counter:=2 to 7 do result:=result+dectohex(GUID.D4[counter]); result:=result+'}'; end; -------------------------------------------------------------------------------