2011-10-18 44 views
0

我有這個問題,這樣做的:我怎麼轉換GUID字符串從WideString映射unicode字符串

function GenGuid: String; 
var 
    guid: TGuid; 
begin 
    CreateGuid(Guid); 
    Result := GuidToString(Guid); 
end; 

它返回字符串格式的GUID。但我如何將寬字符串轉換爲unicodestring?我需要有unicode字符串格式的guid。 非常感謝。

UPDATE

function myguid: string; 
    var 
    i: Integer; 
    s: string; 
    Guid: TGuid; 
    t: byte; 
    begin 
    CreateGuid(Guid); 
    s := GuidToString(Guid); 
    for i := 1 to Length(s) do 
    begin 
     t := Ord(MidStr(s, i, 1)); 
     writeln (t); 
    end; 
    Result := .... // for now not need, just a test 
    end; 

這樣做,T迴流148-124始終,而不是單個字符的ASCII碼。如果我沒有做ord(),然後正確顯示字符。

+2

WideString和UnicodeString是相同的類型。你有什麼問題? – kludg

+0

我正在使用delphi xe2。我只是想將guid轉換爲十六進制。關於轉換,我從Microsoft網站的visual basic中找到了一些腳本。但是讀一個字符爲字符的char,它會返回兩個值:148-124總是。 –

+3

@Serg'WideString'和'UnicodeString'不一樣。 'UnicodeString'使用copy-on-write,'WideString'不包含COM的'BSTR'。 –

回答

4

我不確定這是你真正想要的。

uses 
    ComObj, ActiveX; 

function CreateGuid: string; 
var 
    GUID: TGUID; 
begin 
    Result := ''; 

    if CoCreateGuid(GUID) = S_OK then 
    begin 
    Result := IntToHex(GUID.D1, 8) + 
       IntToHex(GUID.D2, 4) + 
       IntToHex(GUID.D3, 4) + 
       IntToHex(GUID.D4[0], 2) + 
       IntToHex(GUID.D4[1], 2) + 
       IntToHex(GUID.D4[2], 2) + 
       IntToHex(GUID.D4[3], 2) + 
       IntToHex(GUID.D4[4], 2) + 
       IntToHex(GUID.D4[5], 2) + 
       IntToHex(GUID.D4[6], 2) + 
       IntToHex(GUID.D4[7], 2); 
    end; 
end;