Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,845 questions

51,766 answers

573 users

How to convert text to binary code in Pascal

1 Answer

0 votes
program TextToBinary;

uses
  SysUtils;

function IntToBin(Value: Integer): string;
var
  BinStr: string;
begin
  BinStr := '';
  while Value > 0 do
  begin
    if (Value mod 2) = 1 then
      BinStr := '1' + BinStr
    else
      BinStr := '0' + BinStr;
    Value := Value div 2;
  end;
  IntToBin := BinStr;
end;

function TextToBin(txt: string): string;
var
  Bin: string;
  i: Integer;
  CharCode: Integer;
  Binary: string;
begin
  Bin := '';
  for i := 1 to Length(txt) do
  begin
    CharCode := Ord(txt[i]); // Get ASCII value of the character
    Binary := IntToBin(CharCode); // Convert ASCII value to binary
    
    // Ensure binary is padded to 8 bits
    while Length(Binary) < 8 do
      Binary := '0' + Binary;
      
    Bin := Bin + Binary + ' ';
  end;
  TextToBin := Trim(Bin); // Remove trailing space
end;

begin
  Writeln(TextToBin('Pascal'));
end.



(*
run:

01010000 01100001 01110011 01100011 01100001 01101100

*)

 



answered Apr 13, 2025 by avibootz
...