Base64 encoding for utf-8 strings

I have a studio rad xe5 I used indy EncodeString to encode the input string ...

my code looks like this:

procedure TForm5.Button2Click(Sender: TObject);
var
  UTF8: UTF8String;
begin
UTF8 := UTF8Encode(m1.Text);
m2.Text := ind.EncodeString(UTF8);
end;

but the output is wrong for utf-8 inputs

orange  --> b3Jhbmdl  [correct]
book   --> Ym9vaw==   [correct]
سلام  -->  Pz8/Pw==   [wrong]
کتاب  --> Pz8/Pw==   [wrong]
دلفی  --> Pz8/Pw==   [wrong]

for utf-8 for all inputs that he returned the same way! what is wrong with my code and how can I get a good base64 encoding result using utf-8 strings

+4
source share
3 answers

Like @RRUZ, it EncodeString()expects you to specify the byte encoding into which the input will be converted String, and then these octets will be encoded to base64.

UTF8String EncodeString(), UnicodeString XE5, RTL UTF8String UTF-16, UTF8Encode() ( , BTW). , Indy , ​​ ASCII ( GIdDefaultTextEncoding IdGlobal).

orange ( ), سلام ( ).

UTF8String Indy UTF-8 :

procedure TForm5.Button2Click(Sender: TObject);
begin
  m2.Text := TIdEncoderMIME.EncodeString(m1.Text, IndyTextEncoding_UTF8);
end;

DecodeString() , base64. , UnicodeString , :

procedure TForm5.Button3Click(Sender: TObject);
begin
  m1.Text := TIdDecoderMIME.DecodeString(m2.Text, IndyTextEncoding_UTF8);
end;
+7

EncodeString, .

m2.Text := TIdEncoderMIME.EncodeString(UTF8, IndyUTF8Encoding);

(IndyUTF8Encoding IdGlobal)

+4

For RadStudio10 C ++

#include <IdGlobal.hpp> String my_str = L"Շնորհակալություն"; String str = IdEncoderMIME1->EncodeString(my_str ,IndyTextEncoding_UTF8()); my_str = IdDecoderMIME1->DecodeString(str ,IndyTextEncoding_UTF8());

+1
source

Source: https://habr.com/ru/post/1530345/


All Articles