Convert ASCii in Delphi

Question

I'm having problems using delphi to convert ascii code into binary code. ex: A = 65(Dec), 1000001(binary)
Is there any function in Delphi that can convert from dec into binary code?

Answer by Bill DelphiMan

If you want to convert the ASCII code of a character C to a string S, first get the decimal value of the ASCII code with Ord(C). Next, convert this to a string S that contains the binary representation:

S := IntToBin8(Ord(C));

Here's a Delphi source code example for the integer-to-binary function IntToBin8, that returns a string of 8 characters representing the binary form of an integer (such as '01000001' for 65):

function IntToBin8(I: integer): string;
begin
  Result := '';
  while I > 0 do begin
    Result := Chr(Ord('0') + (I and 1)) + Result;
    I := I shr 1;
  end;
  while Length(Result) < 8 do
    Result := '0' + Result;
end;

Thus, after S := IntToBin8(Ord('A'))
S contains '01000001'.