在Delphi中怎样将十进制数转换为二进制的数???

解决方案 »

  1.   


    进制转换    
      HexToBin()十六进制转换二进制 
    BinToHex()二进制转换十六进制IntToHex()StrToInt() 要求在字符串前面添加$即可!把一个整数变成二进制字符串function IntToBinaryStr(TheVal: LongInt): string;varcounter: LongInt;begin{This part is here because we remove leading zeros. Thatmeans that a zero value would return an empty string.}if TheVal = 0 then beginresult := '0';exit;end;result := '';counter := $80000000;{Suppress leading zeros}while ((counter and TheVal) = 0) do begincounter := counter shr 1;if (counter = 0) then break; {We found our first "1".}end;while counter > 0 do beginif (counter and TheVal) = 0 then result := result + '0'else result := result + '1';counter := counter shr 1;end;end;// Binary to Integerfunction BinToInt(Value: string): Integer;vari, iValueSize: Integer;beginResult := 0;iValueSize := Length(Value);for i := iValueSize downto 1 doif Value[i] = '1' then Result := Result + (1 shl (iValueSize - i));end; // Integer to Binaryfunction IntToBin(Value: Longint; Digits: Integer): string;vari: Integer;beginResult := '';for i := Digits downto 0 doif Value and (1 shl i) <> 0 thenResult := Result + '1'elseResult := Result + '0';end;十六进制转换二进制function HexToBin(Hexadecimal: string): string;constBCD: array [0..15] of string =('0000', '0001', '0010', '0011', '0100', '0101', '0110', '0111','1000', '1001', '1010', '1011', '1100', '1101', '1110', '1111');vari: integer;beginfor i := Length(Hexadecimal) downto 1 doResult := BCD[StrToInt('$' + Hexadecimal[i])] + Result;end;八进制和十进制的转换:function OctToInt(Value: string): Longint;vari: Integer;int: Integer;beginint := 0;for i := 1 to Length(Value) dobeginint := int * 8 + StrToInt(Copy(Value, i, 1));end;Result := int;end;function IntToOct(Value: Longint; digits: Integer): string;varrest: Longint;oct: string;i: Integer;beginoct := '';while Value <> 0 dobeginrest := Value mod 8;Value := Value div 8;oct := IntToStr(rest) + oct;end;for i := Length(oct) + 1 to digits dooct := '0' + oct;Result := oct;end;