红衣主教错误地转换为OleVariant。 UInt64还可以

时间:2019-05-13 09:32:11

标签: delphi types variant unsigned-integer

program UInt32ToVariant;

uses
  Vcl.Dialogs, System.SysUtils, System.Variants;

var
  ui32Val: Cardinal;
  si64Val: Int64;
  ui64Val: UInt64;
  v1: Variant;
  v2: OleVariant;
  sb: TStringBuilder;

begin
  sb := TStringBuilder.Create();
  try
    ui32Val := 4234687430;
    si64Val := Int64.MaxValue;
    ui64Val := UInt64.MaxValue;

    v1 := ui32Val;
    v2 := ui32Val;
    sb.AppendLine('Unsigned 32 bit integer:'#9 + Cardinal.ToString(ui32Val));
    sb.AppendLine(#9'to Variant:'#9 + VarToStr(v1));
    sb.AppendLine(#9'to OleVariant:'#9 + VarToStr(v2));
    sb.AppendLine();

    v1 := si64Val;
    v2 := si64Val;
    sb.AppendLine('Signed 64 bit integer:'#9 + Int64.ToString(si64Val));
    sb.AppendLine(#9'to Variant:'#9 + VarToStr(v1));
    sb.AppendLine(#9'to OleVariant:'#9 + VarToStr(v2));
    sb.AppendLine();

    v1 := ui64Val;
    v2 := ui64Val;
    sb.AppendLine('Unsigned 64 bit integer:'#9 + UInt64.ToString(ui64Val));
    sb.AppendLine(#9'to Variant:'#9 + VarToStr(v1));
    sb.AppendLine(#9'to OleVariant:'#9 + VarToStr(v2));

    ShowMessage(sb.ToString());
  finally
    sb.Free();
  end;
end.

结果是:

Unsigned 32 bit integer:    4234687430
    to Variant: 4234687430
    to OleVariant:  -60279866 (Wrong!)

Signed 64 bit integer:  9223372036854775807
    to Variant: 9223372036854775807
    to OleVariant:  9223372036854775807

Unsigned 64 bit integer:    18446744073709551615
    to Variant: 18446744073709551615
    to OleVariant:  18446744073709551615 (Correct again!)

这真的是Delphi中的错误,还是我做错了什么?

我可以假定OleVariant根本不支持无符号整数,但是支持无符号64位整数,不支持32位。

1 个答案:

答案 0 :(得分:4)

这是一个Delphi错误。

它被报告为RSP-18363 Incorrect Conversion Cardinal to OleVariant

错误描述:

  

基数直接转换为OleVariant不正确。   结果不同于将红衣主教转变为变种到OleVariant