Solved

String to Unicode?

Posted on 2004-04-19
3
2,158 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

739 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