Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Converting WideChar/WideString to AnsiChar/AnsiString

547 views
Skip to first unread message

ckoegl

unread,
May 29, 2008, 7:47:06 AM5/29/08
to
Hi,

I cannot find anything definitive in the Delphi docs and in the
relevant newsgroups that answers this simple question:

What is the exact definition of the WideChar to AnsiChar conversion
(and in extension of the WideString to AnsiString conversion)?

I suppose that for all WideChar values that are valid AnsiChar values
(i.e. the ones with Ord-values of 0 to 255) are preserved in the
conversion, but what about the rest?

Cheers,

Christoph

Rob Kennedy

unread,
May 29, 2008, 8:41:40 AM5/29/08
to

They're truncated, I think. Or it's a range-check error.

Converting a WideString to an AnsiString involves converting the
WideString to the current system code page, but I'm pretty sure that
effort is *not* used for converting WideChar to AnsiChar.

And if that's the case, then the conversion is nonsense. Don't do it.

--
Rob

ckoegl

unread,
May 29, 2008, 11:29:58 AM5/29/08
to

Ok, in my case I simply cannot dodge the problem: I have (for whatever
reasons) text
data given as WideString and I need to call given procedures/functions
that only accept
String (which in my case means AnsiString). It is entirely acceptable
that some data
ist lost during the WideString to String conversion, but: (1) All
character information that
can be transferred should be transferred, and (2) I need to know quite
exactly what happens
in case unconvertible character data is encountered.

Well, I guess I could simply try out what happens, but I thought that
maybe you
folks already have deeper experience of what pitfalls may lie ahead of
me ...

One valuable information I already got is that the string conversion
is not simply a sequence
of single character conversions.

Thanks,

Christoph

Maarten Wiltink

unread,
May 29, 2008, 11:39:16 AM5/29/08
to
"ckoegl" <ko...@heidenhain.de> wrote in message
news:e6eb3d02-23e4-4491...@k37g2000hsf.googlegroups.com...

> What is the exact definition of the WideChar to AnsiChar conversion
> (and in extension of the WideString to AnsiString conversion)?

That depends on the conversion you use. Windows has a built-in function
whose name escapes me at the moment; something like UnicodeToMultiByte.
It shouldn't be too hard to find. IIAMN, Delphi uses it when you assign
a WideString to an AnsiString.


> I suppose that for all WideChar values that are valid AnsiChar values
> (i.e. the ones with Ord-values of 0 to 255) are preserved in the
> conversion, but what about the rest?

The above-mentioned Windows function replaces them with question marks.
Which is not very good, but about the best you can do short of throwing
an exception.

Groetjes,
Maarten Wiltink


Jamie

unread,
May 29, 2008, 12:46:52 PM5/29/08
to
I think Delphi does some converting on it's own how ever..
look at these

WideCharToMultiByte
MultiByteToWideChar
FoldString
IsDBCSLeadByte

These are in the windows help, they should get you started.
http://webpages.charter.net/jamie_5"

Rudy Velthuis

unread,
May 29, 2008, 7:07:27 PM5/29/08
to
ckoegl wrote:

> Hi,
>
> I cannot find anything definitive in the Delphi docs and in the
> relevant newsgroups that answers this simple question:
>
> What is the exact definition of the WideChar to AnsiChar conversion
> (and in extension of the WideString to AnsiString conversion)?

The RTL basically uses this:

function CharFromWChar(CharDest: PChar; DestBytes: Integer; const
WCharSource: PWideChar; SrcChars: Integer): Integer;
begin
Result := WideCharToMultiByte(DefaultSystemCodePage, 0,
WCharSource, SrcChars,
CharDest, DestBytes, nil, nil);
end;


--
Rudy Velthuis http://rvelthuis.de

"Beware of bugs in the above code; I have only proven it correct,
not tried it." -- Donald E. Knuth.

Ivan Levashew

unread,
May 29, 2008, 8:41:05 PM5/29/08
to
ckoegl пишет:

>
> Ok, in my case I simply cannot dodge the problem: I have (for
> whatever reasons) text data given as WideString and I need to call
> given procedures/functions that only accept String (which in my case
> means AnsiString). It is entirely acceptable that some data ist lost
> during the WideString to String conversion, but: (1) All character
> information that can be transferred should be transferred, and (2) I
> need to know quite exactly what happens in case unconvertible
> character data is encountered.
>

In the case these functions doesn't operate with the outer world (pure
string processing) you can probably use UTF-8.

--
If you want to get to the top, you have to start at the bottom

0 new messages