Converting decimal/integer to binary - how and why it works the way it does?

后端 未结 6 1421
無奈伤痛
無奈伤痛 2020-12-11 10:15

As already asked David in a comment of an answer here, I\'m really interested on how this function works, since I can\'t seem to get the same (correct) values if changing re

6条回答
  •  甜味超标
    2020-12-11 10:33

    function IntToBin2(Value: Integer): string;
    var
      i, pol: Integer;
    begin
      Result:= '';
      for i := 1 to Value do
      begin
        pol:= Value div 2;
        Result:= IntToStr(Value - pol * 2) + Result;
        Value:= pol;
        if pol = 0 then
          Break;
      end;
    end;

提交回复
热议问题