麻烦各位帮我写一个函数,传入一个Int64的参数,然后返回一个0101的二进制字符串长度为32。
在线等,谢谢。

解决方案 »

  1.   

    function IntToBin(i: Int64): string;
    var
      n: Integer;
    begin
      n := Abs(i);
      result := '';
      while n <> 0 do
      begin
        result := IntToStr(n mod 2) + result;
        n := n div 2;
      end;
      result := StringOfChar('0', 32 - Length(result)) + result;
    end;
      

  2.   

    delphi自己有:
    Returns a binary string representation for an Integer value.
    function IntToBin(Value: cardinal): string;ParametersValue: cardinalCardinal value to be converted.
    ReturnsString - Binary representation of the Integer value.
    DescriptionIntToBin is a String function used to construct a binary representation of a 32-bit Integer value. The return value for IntToBin will contain a string of "0" or "1" characters for each of the bits in the integer Value.
      

  3.   

    function HextoBin1(Hex:string):string;
    const
        BOX: array [0..15] of string =
             ('0000','0001','0010','0011',
              '0100','0101','0110','0111',
              '1000','1001','1010','1011',
              '1100','1101','1110','1111');
    var
        i:integer;
    begin
        for i:=Length(Hex) downto 1 do
            Result:=BOX[StrToInt('$'+Hex[i])]+Result;
    end;str := HexToBin1(IntToHex(i,8));
      

  4.   

    二楼所说帮助中找不到,在IdGlobal中,须加入此单元,下面是源码:
    function IntToBin(Value: cardinal): string;
    var
      i: Integer;
    begin
      SetLength(result, 32);
      for i := 1 to 32 do
      begin
        if ((Value shl (i-1)) shr 31) = 0 then
          result[i] := '0'  {do not localize}
        else
          result[i] := '1'; {do not localize}
      end;
    end;