Solved

String to Unicode?

Posted on 2004-04-19
3
2,134 Views
Last Modified: 2007-12-19
Hi.
I am trying to convert a string/pchar to unicode. The problem is when I call MultiByteToWideString I get a "privilege" error. I am wondering why I can not call this function when it is possible to call them from C++ Compiler. Is there another way to convert to unicode? (UTF8ToUnicode function gives same error)

Thanks!
0
Comment
Question by:GiulianoB
3 Comments
 
LVL 12

Expert Comment

by:Lee_Nover
Comment Utility
if you have an UTF8 string then use UTF8Decode()
for ansi strings you can use StringToOleStr()
0
 
LVL 12

Expert Comment

by:Ivanov_G
Comment Utility
function Utf8ToUnicode(Dest: PWideChar; Source: PChar; MaxChars: Integer): Integer;
var
  count: Integer;
  c: Byte;
  wc: Integer;
begin
  if Source = nil then
  begin
    Result := 0;
    Exit;
  end;
  Result := -1;
  count := 0;
  if Dest <> nil then
  begin
  while (Source^ <> #0) and (count < MaxChars) do
  begin
    wc := Integer(Source^);
    Inc(Source);
    if (wc and $80) <> 0 then
    begin
    wc := wc and $3F;
    if (wc and $20) <> 0 then
    begin
      c := Byte(Source^);
      Inc(Source);
      if (c and $C0) <> $80 then  Exit;     // malformed trail byte or out of range char
      wc := (wc shl 6) or (c and $3F);
    end;
    c := Byte(Source^);
    Inc(Source);
    if (c and $C0) <> $80 then Exit;       // malformed trail byte

    Dest[count] := WideChar((wc shl 6) or (c and $3F));
    end
    else
    Dest[count] := WideChar(wc);
    Inc(count);
  end;
  Dest[count] := #0;
  end
  else
  begin
  while Source^ <> #0 do
  begin
    c := Byte(Source^);
    Inc(Source);
    if (c and $80) <> 0 then
    begin
    if (c and $F0) = $F0 then Exit;  // too many bytes for UCS2
    if (c and $40) = 0 then Exit;    // malformed lead byte

    if (Byte(Source^) and $C0) <> $80 then Exit;  // malformed trail byte
    Inc(Source);
    if ((c and $20) <> 0) and ((Byte(Source^) and $C0) <> $80) then Exit; // malformed trail byte
    Inc(Source);
    end;
    Inc(count);
  end;
  end;
  Result := count;
end;
0
 
LVL 12

Accepted Solution

by:
andrewjb earned 90 total points
Comment Utility
Just assign to a WideString, which is unicode..

AnsiString x;
WideString y;

x := 'This is 1-byte ascii';
y := x;

// y is now in unicode.

And you can use y.c_bstr() to get at the actual bytes of the unicode if you need them...
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This video discusses moving either the default database or any database to a new volume.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now