在Indy 10和Delphi中编码 [英] Encoding in Indy 10 and Delphi

查看:429
本文介绍了在Indy 10和Delphi中编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Delphi中使用Indy 10。以下是我的代码,该代码使用Indy的 EncodeString 方法编码字符串。

I am using Indy 10 with Delphi. Following is my code which uses EncodeString method of Indy to encode a string.

var
  EncodedString : String;
  StringToBeEncoded : String;
  EncoderMIME: TIdEncoderMIME;
....
....
EncodedString := EncoderMIME.EncodeString(StringToBeEncoded);

我在编码字符串中没有获得正确的值。

I am not getting the correct value in encoded sting.

推荐答案


IndyTextEncoding_OSDefault 的目的是什么?

这是 IndyTextEncoding_OSDefault 的源代码。

function IndyTextEncoding_OSDefault: IIdTextEncoding;
begin
  if GIdOSDefaultEncoding = nil then begin
    LEncoding := TIdMBCSEncoding.Create;
    if InterlockedCompareExchangeIntf(IInterface(GIdOSDefaultEncoding), LEncoding, nil) <> nil then begin
      LEncoding := nil;
    end;
  end;
  Result := GIdOSDefaultEncoding;
end;

请注意,为简单起见,我删除了.net条件代码。此代码大部分是安排单例线程安全的。返回的实际值是通过调用 TIdMBCSEncoding.Create 来综合的。让我们看一下。

Note that I stripped out the .net conditional code for simplicity. Most of this code is to arrange singleton thread-safety. The actual value returned is synthesised by a call to TIdMBCSEncoding.Create. Let's look at that.

constructor TIdMBCSEncoding.Create;
begin
  Create(CP_ACP, 0, 0);
end;

再次,我删除了不适用于Windows设置的条件代码。现在, CP_ACP A 活动 C ode P age ,即当前系统的Windows ANSI代码页。因此,至少在Windows上, IndyTextEncoding_OSDefault 是当前系统Windows ANSI代码页的编码。

Again I've remove conditional code that does not apply to your Windows setting. Now, CP_ACP is the Active Code Page, the current system Windows ANSI code page. So, on Windows at least, IndyTextEncoding_OSDefault is an encoding for the current system Windows ANSI code page.


为什么使用 IndyTextEncoding_OSDefault 会产生与我的Delphi 7代码相同的行为?

Why did using IndyTextEncoding_OSDefault give the same behaviour as my Delphi 7 code?

这是因为 TEncoderMIME.EncodeString 的Delphi 7 / Indy 9代码不会执行任何代码页转换, MIME对输入字符串进行编码,就好像它是字节数组一样。由于Delphi 7字符串是在活动的ANSI代码页中编码的,因此与将 IndyTextEncoding_OSDefault 传递给 TEncoderMIME.EncodeString 在您的Unicode版本代码中。

That's because the Delphi 7 / Indy 9 code for TEncoderMIME.EncodeString does not perform any code page transformation and MIME encodes the input string as though it were a byte array. Since the Delphi 7 string is encoded in the active ANSI code page, this has the same effect as passing IndyTextEncoding_OSDefault to TEncoderMIME.EncodeString in your Unicode version of the code.


<$有什么区别c $ c> IndyTextEncoding_Default IndyTextEncoding_OSDefault

这是 IndyTextEncoding_OSDefault 的源代码:

function IndyTextEncoding_Default: IIdTextEncoding;
var
  LType: IdTextEncodingType;
begin
  LType := GIdDefaultTextEncoding;
  if LType = encIndyDefault then begin
    LType := encASCII;
  end;
  Result := IndyTextEncoding(LType);
end;

这将返回由 GIdDefaultTextEncoding 。默认情况下, GIdDefaultTextEncoding encASCII 。因此,默认情况下, IndyTextEncoding_Default 会产生ASCII编码。

This returns an encoding that is determined by the value of GIdDefaultTextEncoding. By default, GIdDefaultTextEncoding is encASCII. And so, by default, IndyTextEncoding_Default yields an ASCII encoding.

除此之外,您还应该问自己要使用哪种编码。依靠默认值会使您受这些默认值的支配。如果这些默认设置不满足您的要求,该怎么办。特别是因为默认值不是Unicode编码,因此仅支持有限范围的字符。而且,这还取决于系统设置。

Beyond all this you should be asking yourself which encoding you want to be using. Relying on default values leaves you at the mercy of those defaults. What if those defaults don't do what you want to do. Especially as the defaults are not Unicode encodings and so support only a limited range of characters. And what's more are dependent on system settings.

如果您希望对国际文字进行编码,通常会选择使用UTF-8编码。

If you wish to encode international text, you would normally choose to use the UTF-8 encoding.

需要说明的另一点是,您正在调用 EncodeString ,就像它是实例一样方法,但实际上是一个类方法。您可以删除 EncoderMIME 并调用 TEncoderMIME.EncodeString 。或者保留 EncoderMIME 并调用 EncoderMIME.Encode

One other point to make is that you are calling EncodeString as though it were an instance method, but it is actually a class method. You can remove EncoderMIME and call TEncoderMIME.EncodeString. Or keep EncoderMIME and call EncoderMIME.Encode.

这篇关于在Indy 10和Delphi中编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆