Solved

String to Unicode?

Posted on 2004-04-19
3
2,137 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
ID: 10865882
if you have an UTF8 string then use UTF8Decode()
for ansi strings you can use StringToOleStr()
0
 
LVL 12

Expert Comment

by:Ivanov_G
ID: 10866661
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
ID: 10867121
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

863 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

26 Experts available now in Live!

Get 1:1 Help Now