2
votes

I encoded a text in a TMemo using base64 encoding tools ( like Indy tools ) but after decoding the result text, I just get some " ? " characters instead of the correct text . the original text is in persian language .

THE QUESTION IS :

I can see the decoded text if I set the windows"language for non unicode programs " on persian language . but I wanna make the decoded text visible without changing the " windows non unicode ... " .

Its about two weeks Im stuck on this ! I tried "UTF8ToWideString" , "UTF8ToUTF16","UTF8ToUnicodeString","UTF8ToString","UTF8Decode" and "UTF8EncodeToShortString" but non of them worked.

Encoding >

  var input,output:TstringStream;
    begin
    input:=nil;
    output:=nil;
    input:=TstringStream.Create;
    output:=TstringStream.Create;
    memo1.Lines.SaveToStream(input);

    input.Position:=0;
    encoder.Encode(input,output);
    output.Position:=0;
    memo2.Clear;
    memo2.Lines.LoadFromStream(output);
    input.Free;
    output.Free
    end; 

Decoding >>

var input,output:TStringStream;
  begin
  memo3.Clear;
  input:=nil;
  output:=nil;
  input:=TStringStream.Create();
  output:=TStringStream.Create();
  memo2.Lines.SaveToStream(input);
  input.Position:=0;
  decoder.DecodeBegin(output);
  decoder.Decode(input);
  decoder.DecodeEnd;
  output.Position:=0 ;
  memo3.Lines.Add(output.DataString);
   end;

I also tried delphi internal encoding tools . as following

// encoding >
memo2.Lines.Add(EncodeString(memo1.Lines.Text))
// decoding>

 memo3.lines.add(DecodeString(memo2.Lines.Text)) ;
1
Maybe your base64 encoding tools can't cope with the original unicode and the problem is in the encoding not the decoding. Try adding just one character to the Tmemo (one of the problem characters) and see how long the resulting base64 string is. Is it long enough to have 16 bits, or is it just 8? - weston
I don't think the encoder being malfunctioning . cuz I used both delphi internal encoding tool (EncdDecd unit) and IdEncode/DecodeerMime . - Hesi
I tried what you said . I dont know much about such stuff so can't say if the resulting sring is 8 or 16 bits . if I encode just one character I get this > Pw== . - Hesi
2 '='s indicates the 4 characters will decode to a single byte. See en.wikipedia.org/wiki/Base64#Decoding_Base64_with_padding This is proof that that the information has been lost before or during encoding. - weston
thank you very much . But Whats you idea to solve the issue ?! - Hesi

1 Answers

4
votes

Try something like this instead:

memo2.Text := TIdEncoderMIME.EncodeString(memo1.Text, IndyUTF8Encoding);

memo3.Text := TIdEncoderMIME.DecodeString(memo2.Text, IndyUTF8Encoding);