关于4位组成一个十六位数的方法

来源:互联网 发布:《算法》第4版pdf 编辑:程序博客网 时间:2024/05/17 04:10

 procedure TForm1.Button1Click(Sender: TObject);

const
  cHexChars: array[0..15] of Char = '0123456789ABCDEF';
var
  S: string;
  I, J: Integer;
  A: array of Byte;
  P: PChar;
  vTickCount: Longword; // 计时
begin
  s := ''; //存放结果
  SetLength(A, 10240000); // 测试数据开大一些
  SetLength(S, Length(A) div 4);
  for I := Low(A) to High(A) do //生成测试数据
    A[I] := Random(2);
  P := PChar(S);
  vTickCount := GetTickCount;
  {$R-,O-}
  for I := 0 to Length(S) - 1 do //少循环N次
  begin
    J := I shl 2;
    P^ := cHexChars[
      A[J + 0] shl 3 or
      A[J + 1] shl 2 or
      A[J + 2] shl 1 or
      A[J + 3] shl 0
    ];
    Inc(P);
  end;
  {$R+,O+}

  form1.Caption := IntToStr(GetTickCount - vTickCount); // 输出处理时间

  RichEdit1.Text :=Copy(S, 1, 10240); // 输出部分结果

end;