1

i have this problem, doing:

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

It return a guid in string format. But i how do to convert a widestring to unicodestring? I need to have the guid in unicode string format. Thanks very much.

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;

Doing so, t return 148-124 always and not ascii of single character. If i not do ord( ) then display char correctly.

Marcello Impastato
  • 2,263
  • 5
  • 30
  • 52
  • 2
    WideString and UnicodeString are the same type. What is your problem? – kludg Oct 18 '11 at 10:24
  • I am using delphi xe2. I simply want convert guid to hexadecimal. About conversion i have found something script in visual basic from microsoft website. But reading char for char with a cycle for, it return couple of value : 148-124 always. – Marcello Impastato Oct 18 '11 at 10:32
  • 3
    @Serg `WideString` and `UnicodeString` are not the same. `UnicodeString` uses copy-on-write, `WideString` does not and wraps a `BSTR` for COM. – David Heffernan Oct 18 '11 at 10:50
  • @Marcello I can't understand your question. `String` is synonymous with `UnicodeString`. You already have a `UnicodeString`. Please clarify. – David Heffernan Oct 18 '11 at 10:51
  • hello, i have update code, for give better idea, maybe so i can to more right. sorry for mine english :( – Marcello Impastato Oct 18 '11 at 11:04
  • I still have no idea what you want. Please give us an example of what the string should look like. – David Heffernan Oct 18 '11 at 11:07
  • Ok, i want make an guid and convert it in hexadecimal, but the result need to be in unicode string. About conversion, i have found something here: http://support.microsoft.com/kb/325648. Using delphi xe2, when i try to read single chars, not return ordinal value of the character that reading, but alvays value as 148-124. I have tried to use Copy to place midstr, but result is same. – Marcello Impastato Oct 18 '11 at 11:16
  • @Marcello, but that's what [GuidToString](http://docwiki.embarcadero.com/VCL/en/SysUtils.GUIDToString) do. Please provide the example of how you want to format that GUID. – TLama Oct 18 '11 at 11:22
  • About GuidToString, http://docwiki.embarcadero.com/VCL/en/SysUtils.GUIDToString. It convert Guid from TGuid format to string. I have this guid: {XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX} and i want to have: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX in equivalent hexadecimal number, but as unicode string. For strip chars {, }, and -, i need read all string char for char, but when i read single char it not return me equivaent ordinal value of X. – Marcello Impastato Oct 18 '11 at 11:38

1 Answers1

5

I'm not sure if that's what you really want.

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;
TLama
  • 75,147
  • 17
  • 214
  • 392