我如何将 guid 字符串从 Widestring 转换为 unicode 字符串

Mar*_*ato 1 delphi delphi-xe2

我有这个问题,正在做:

function GenGuid: String;
var
  guid: TGuid;
begin
  CreateGuid(Guid);
  Result := GuidToString(Guid);
end;
Run Code Online (Sandbox Code Playgroud)

它返回字符串格式的 guid。但我如何将宽字符串转换为 unicodestring?我需要 unicode 字符串格式的 guid。非常感谢。

更新

  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;
Run Code Online (Sandbox Code Playgroud)

这样做,t 总是返回 148-124,而不是单个字符的 ASCII。如果我不执行ord(),则正确显示字符。

TLa*_*ama 5

我不确定这是否是你真正想要的。

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;
Run Code Online (Sandbox Code Playgroud)