program CharacterBitsProgram;
{$mode objfpc}{$H+}
{
Converts a character to its 8-bit binary representation.
}
function CharToBits(const Ch: Char): String;
var
Value: Byte;
I: Integer;
begin
Result := '';
{ Convert the character to its numeric value }
Value := Ord(Ch);
{ Extract bits from most significant to least significant }
for I := 7 downto 0 do
begin
if ((Value shr I) and 1) = 1 then
Result := Result + '1'
else
Result := Result + '0';
end;
end;
{
Prints a character and its binary representation.
}
procedure PrintCharacterBits(const Ch: Char);
begin
WriteLn('Character: ''', Ch, '''');
WriteLn('Decimal : ', Ord(Ch));
WriteLn('Binary : ', CharToBits(Ch));
end;
begin
{ Usage examples }
PrintCharacterBits('A');
WriteLn;
PrintCharacterBits('a');
WriteLn;
PrintCharacterBits('5');
WriteLn;
PrintCharacterBits('@');
end.
{
run:
Character: 'A'
Decimal : 65
Binary : 01000001
Character: 'a'
Decimal : 97
Binary : 01100001
Character: '5'
Decimal : 53
Binary : 00110101
Character: '@'
Decimal : 64
Binary : 01000000
}