delphi tcp utf8_tcp/ip通讯中出现乱码的问题(200分)

该博客主要讨论了在Delphi TCP/IP通讯中遇到的UTF8乱码问题,并提供了使用mail2000控件和Base64转换函数的解决方法。通过示例代码展示了如何将Base64字符串转换为UTF8字符串,以及如何从特定编码格式的标题中提取正确内容。
摘要由CSDN通过智能技术生成

用mail2000控件代替,还前前几天aolo给我一个单元文件,现在给你

unit Unit1;

interface

uses

Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

Dialogs, StdCtrls;

type

TForm1 = class(TForm)

Button1: TButton;

procedure Button1Click(Sender: TObject);

private

{ Private declarations }

public

{ Public declarations }

end;

var

Form1: TForm1;

implementation

{$R *.dfm}

function Base64ToString(const Value : string): string;

var

x, y, n, l: Integer;

d: array[0..3] of Byte;

Table : string;

begin

Table :=

#$40 +#$40 +#$40 +#$40 +#$40 +#$40 +#$40 +#$40 +#$40 +#$40 +#$3E +#$40

+#$40 +#$40 +#$3F +#$34 +#$35 +#$36 +#$37 +#$38 +#$39 +#$3A +#$3B +#$3C

+#$3D +#$40 +#$40 +#$40 +#$40 +#$40 +#$40 +#$40 +#$00 +#$01 +#$02 +#$03

+#$04 +#$05 +#$06 +#$07 +#$08 +#$09 +#$0A +#$0B +#$0C +#$0D +#$0E +#$0F

+#$10 +#$11 +#$12 +#$13 +#$14 +#$15 +#$16 +#$17 +#$18 +#$19 +#$40 +#$40

+#$40 +#$40 +#$40 +#$40 +#$1A +#$1B +#$1C +#$1D +#$1E +#$1F +#$20 +#$21

+#$22 +#$23 +#$24 +#$25 +#$26 +#$27 +#$28 +#$29 +#$2A +#$2B +#$2C +#$2D

+#$2E +#$2F +#$30 +#$31 +#$32 +#$33 +#$40 +#$40 +#$40 +#$40 +#$40 +#$40;

SetLength(Result, Length(Value));

x := 1;

l := 1;

while x < Length(Value) do

begin

for n := 0 to 3 do

begin

if x > Length(Value) then

d[n] := 64

else

begin

y := Ord(Value[x]);

if (y < 33) or (y > 127) then

d[n] := 64

else

d[n] := Ord(Table[y - 32]);

end;

Inc(x);

end;

Result[l] := Char((D[0] and $3F) shl 2 + (D[1] and $30) shr 4);

Inc(l);

if d[2] <> 64 then

begin

Result[l] := Char((D[1] and $0F) shl 4 + (D[2] and $3C) shr 2);

Inc(l);

if d[3] <> 64 then

begin

Result[l] := Char((D[2] and $03) shl 6 + (D[3] and $3F));

Inc(l);

end;

end;

end;

Dec(l);

SetLength(Result, l);

end;

function StringToBase64(const Value: string): string;

var

c: Byte;

n, l: Integer;

Count: Integer;

DOut: array[0..3] of Byte;

Table : string;

begin

Table :=

'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';

setlength(Result, ((Length(Value) + 2) div 3) * 4);

l := 1;

Count := 1;

while Count <= Length(Value) do

begin

c := Ord(Value[Count]);

Inc(Count);

DOut[0] := (c and $FC) shr 2;

DOut[1] := (c and $03) shl 4;

if Count <= Length(Value) then

begin

c := Ord(Value[Count]);

Inc(Count);

DOut[1] := DOut[1] + (c and $F0) shr 4;

DOut[2] := (c and $0F) shl 2;

if Count <= Length(Value) then

begin

c := Ord(Value[Count]);

Inc(Count);

DOut[2] := DOut[2] + (c and $C0) shr 6;

DOut[3] := (c and $3F);

end

else

begin

DOut[3] := $40;

end;

end

else

begin

DOut[2] := $40;

DOut[3] := $40;

end;

for n := 0 to 3 do

begin

Result[l] := Table[DOut[n] + 1];

Inc(l);

end;

end;

end;

function GetTitle(const Value: string): string;

var

iPos: integer;

begin

Result := Value;

if Copy(Value, 1, 2) <> '=?' then exit;

//'?B?'前面的都要去掉

iPos := Pos('?B?', Value);

Inc(iPos, 3);

//最后的'?='也要去掉

Result := Copy(Value, iPos, Length(Value) - iPos - 1);

Result := Base64ToString(Result);

end;

procedure TForm1.Button1Click(Sender: TObject);

begin

ShowMessage(GetTitle('=?gb2312?B?YXNkZnNhZGZkc2Fm1tC5+g==?='));

end;

end.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值