Delphi 7 and decode UTF-8 base64 -
in delphi 7, have widestring encoded base64(that received web service widestring result) :
pd94bwwgdmvyc2lvbj0ims4wij8+dqo8c3ryaw5nptiq2lpyqjwvc3ryaw5npg==
when decoded it, result not utf-8:
<?xml version="1.0"?> <string>طھط³طھ</string>
but when decoded base64decode.org, result true :
<?xml version="1.0"?> <string>تست</string>
i have use encddecd unit decodestring function.
the problem have using decodestring
. function, in delphi 7, treats decoded binary data being ansi encoded. , problem text utf-8 encoded.
to continue encddecd
unit have couple of options. can switch decodestream
. instance, code produce utf-8 encoded text file data:
{$apptype console} uses classes, encddecd; const data = 'pd94bwwgdmvyc2lvbj0ims4wij8+dqo8c3ryaw5nptiq2lpyqjwvc3ryaw5npg=='; var input: tstringstream; output: tfilestream; begin input := tstringstream.create(data); try output := tfilestream.create('c:\desktop\out.txt', fmcreate); try decodestream(input, output); output.free; end; input.free; end; end.
or continue decodestring
, decode utf-8 text widestring
. this:
{$apptype console} uses classes, encddecd; const data = 'pd94bwwgdmvyc2lvbj0ims4wij8+dqo8c3ryaw5nptiq2lpyqjwvc3ryaw5npg=='; var utf8: ansistring; wstr: widestring; begin utf8 := decodestring(data); wstr := utf8decode(utf8); end.
if content of file can represented in application's prevailing ansi locale can convert widestring
plain ansistring
.
var wstr: widestring; str: string; // alias ansistring .... wstr := ... // before str := wstr;
however, don't think using ansi encoded text going lead fruitful programming life. encourage embrace unicode solutions.
judging content of decoded data, xml. handed xml parser. xml parsers accept utf-8 encoded data, quite can base64 decode memory stream using decodestream
, hand stream off xml parser. way don't need decode utf-8 text , can let xml parser deal aspect.
Comments
Post a Comment