Author: Jonas Bilinkevicius
How to convert a WideString to a String
Answer:
Converts Unicode string to Ansi string using specified code page.
@param ws Unicode string.
@param codePage Code page to be used in conversion.
@returns Converted ansi string.
1 function WideStringToString(const ws: WideString; codePage: Word):
2 AnsiString;
3 var
4 l: integer;
5 begin
6 if ws = ' then
7 Result := '
8 else
9 begin
10 l := WideCharToMultiByte(codePage,
11 WC_COMPOSITECHECK or WC_DISCARDNS or WC_SEPCHARS or WC_DEFAULTCHAR,
12 @ws[1], -1, nil, 0, nil, nil);
13 SetLength(Result, l - 1);
14 if l > 1 then
15 WideCharToMultiByte(codePage,
16 WC_COMPOSITECHECK or WC_DISCARDNS or WC_SEPCHARS or WC_DEFAULTCHAR,
17 @ws[1], -1, @Result[1], l - 1, nil, nil);
18 end;
19 end; { WideStringToString }
20
21 Converts Ansi string to Unicode string using specified code page.
22 @param s Ansi string.
23 @param codePage Code page to be used in conversion.
24 @returns Converted wide string.
25
26 function StringToWideString(const s: AnsiString; codePage: Word): WideString;
27 var
28 l: integer;
29 begin
30 if s = ' then
31 Result := '
32 else
33 begin
34 l := MultiByteToWideChar(codePage, MB_PRECOMPOSED, PChar(@s[1]), -1, nil, 0);
35 SetLength(Result, l - 1);
36 if l > 1 then
37 MultiByteToWideChar(CodePage, MB_PRECOMPOSED, PChar(@s[1]),
38 -1, PWideChar(@Result[1]), l - 1);
39 end;
40 end; { StringToWideString }
|