How to Convert a Byte Value to a Binary String Representation

Delphi offers the IntToHex() function to convert integer values to hex string representations but what if you need to convert a number to a binary not a hex? Well, here’s the code for converting byte values to strings:

  function IntToBin(btValue: Byte): string;

     function Next(btJ: Byte): string;
     begin
       if btValue and btJ = 0 then
         Result := '0'
       else
         Result := '1';
     end;

  begin
    Result := Next(128) + Next(64) + Next(32) + Next(16) + Next(8) + Next(4) + Next(2) + Next(1);
  end;

Tags: , , , , , , , , , ,

No Comments Yet

You can be the first to comment!

Leave a comment

OpenID Login

Standard Login