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

Semrush - keyword research tool

Create your online store today with Shopify

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth

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

Disclosure: My content contains affiliate links.

43,227 questions

56,129 answers

573 users

How to convert binary digits to a byte array in Pascal

1 Answer

0 votes
program BinaryToByteArray;

uses
  SysUtils;

function IsBinaryChar(c: Char): Boolean;
begin
  IsBinaryChar := (c = '0') or (c = '1');
end;

function BinaryToByteArray(const BinaryString: string): TBytes;
var
  i, j, byteValue: Integer;
  byteStr: string;
begin
  if Length(BinaryString) mod 8 <> 0 then
  begin
    Writeln('Error: Binary string length must be a multiple of 8.');
    Halt(1);
  end;

  SetLength(BinaryToByteArray, Length(BinaryString) div 8);

  for i := 0 to High(BinaryToByteArray) do
  begin
    byteStr := Copy(BinaryString, i * 8 + 1, 8);
    byteValue := 0;
    for j := 1 to 8 do
    begin
      if not IsBinaryChar(byteStr[j]) then
      begin
        Writeln('Error: Invalid character "', byteStr[j], '" in binary string.');
        Halt(1);
      end;
      byteValue := (byteValue shl 1) or (Ord(byteStr[j]) - Ord('0'));
    end;
    BinaryToByteArray[i] := byteValue;
  end;
end;

var
  BinaryString: string;
  ByteArray: TBytes;
  i: Integer;
begin
  BinaryString := '10101110111010101110101001001011';

  ByteArray := BinaryToByteArray(BinaryString);
  Write('Byte Array: ');
  for i := 0 to High(ByteArray) do
    Write(ByteArray[i], ' ');
  Writeln;
end.




(*
run:
  
Byte Array: 174 234 234 75 

*)

 



answered Aug 4, 2025 by avibootz
...