Solved

String to Unicode?

Posted on 2004-04-19
3
2,162 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

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses
Course of the Month5 days, 1 hour left to enroll

636 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