unit unit1;
{$MODE Delphi}
interface
uses
LCLIntf, LCLType, LMessages, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Menus;
type
{ TForm1 }
TForm1 = class(TForm)
cb16: TCheckBox;
cb17: TCheckBox;
cb18: TCheckBox;
cb19: TCheckBox;
cb20: TCheckBox;
cb21: TCheckBox;
cb22: TCheckBox;
cb23: TCheckBox;
cbReset: TCheckBox;
cb5: TCheckBox;
cb4: TCheckBox;
cb3: TCheckBox;
cb2: TCheckBox;
cb1: TCheckBox;
cb0: TCheckBox;
cb15: TCheckBox;
cb14: TCheckBox;
cb13: TCheckBox;
cb12: TCheckBox;
cb11: TCheckBox;
cb10: TCheckBox;
cb9: TCheckBox;
cb8: TCheckBox;
cb7: TCheckBox;
cb6: TCheckBox;
edBinary2: TEdit;
edBinary3: TEdit;
edBinary4: TEdit;
edDec2: TEdit;
edDec3: TEdit;
edDec4: TEdit;
edHex1: TEdit;
edHex2: TEdit;
edHex3: TEdit;
edHex4: TEdit;
edHex5: TEdit;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
lbBinary1: TLabel;
lbBinary2: TLabel;
lbBinary3: TLabel;
lbDec1: TLabel;
lbDec2: TLabel;
lbDec3: TLabel;
lbHex: TLabel;
edDec1: TEdit;
lbDec: TLabel;
lbBinary: TLabel;
edBinary1: TEdit;
lbHex1: TLabel;
lbHex2: TLabel;
lbHex3: TLabel;
procedure cb15Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.lfm}
var
BinaryStr: AnsiString = '0000000000000000'+
'00000000';
C32, CheckBoxes: CARDINAL;
CheckBoxs1,CheckBoxs2 : word;
function BinToDec(const AValue : AnsiString): Integer;
var
I: Integer;
begin
Result := 0;
for I := 1 to Length(AValue) do
Result := Result * 2 + StrToInt(AValue[I]);
end;
function BinToHex(AValue: AnsiString): AnsiString;
const
Chrs : array [0..15] of AnsiChar = '0123456789ABCDEF';
var
Dec: Integer;
begin
Dec := BinToDec(AValue);
Result := '';
while Dec > 0 do
begin
Result := Chrs[Dec - Dec shr 4 shl 4] + Result;
Dec := Dec shr 4;
end;
end;
const
ChrsX : array [0..31] of AnsiChar = '0123456789ABCDEF'+
'GHJKLMNPQRSTUVWX';
Charz : array [0..63] of AnsiChar = '0123456789ABCDEF'+
'GHJKLMNPQRSTUVWX'+
'YZabcdefghijkmno'+
'pqrstuvwyz?-^&@#';
function ByteToSN(b,a: byte): string;
var i : byte;
begin
assert(b <= $F, 'only support 4bit');
assert(a <= $F, 'only support 4bit');
result := ChrsX[ b + (a shl 4) ];
end;
function WordToSN(b,a: word ): string;
var i, allbits, onegroup, groups : word;
begin
result := '';
allbits := 16;
onegroup := 4;
groups := allbits div onegroup;
for i := 0 to groups - 1 do
begin
result := ByteToSN(b and $F, a and $1 ) + result;
b := b shr onegroup;
a := a shr 1;
end;
end;
function LongWordToSN(c: Cardinal; Chars:array of AnsiChar ): string;
var i,l : byte;
begin
l := length(Chars);
result := '';
while c > 0 do
begin
i := c mod l;
result := Chars[ i ] + result;
c := c div l;
end;
while length(result) < 4 do
result := '0' + result;
end;
procedure TForm1.cb15Click(Sender: TObject);
var T,i,L,Lhex, K,M,N : integer;
BC : string; //backward compatible
AD : string; // addditional
J,sk,sm : string;
begin
//L := 16;
L := length(BinaryStr);
T := (Sender as TCheckBox).Tag;
if (Sender as TCheckBox).Checked then
BinaryStr[L - T] := '1'
else
BinaryStr[L - T] := '0';
{
edBinary1.Text := BinaryStr;
CheckBoxes := BinToDec(BinaryStr);
edDec1.Text := IntToStr(CheckBoxes);
edHex1.Text := '$' + BinToHex(BinaryStr);
}
LHex := 16;
// 1. Backward compatible 16 checkboxes
BC := copy(BinaryStr, L-LHex +1, L);
edBinary1.Text := BC;
CheckBoxs1 := BinToDec(BC);
edDec1.Text := IntToStr(CheckBoxs1);
edHex1.Text := '$' + inttohex(CheckBoxs1,4);
// 2. Additional, plain 4 checkboxes.
AD := copy(BinaryStr, 1, L-LHex);
edBinary2.Text := AD;
CheckBoxs2 := BinToDec(AD);
edDec2.Text := IntToStr(CheckBoxs2);
edHex2.Text := '$' + inttohex(CheckBoxs2,4);
// 3. All Altogether, backward compatible
J := '';
for i := 1 to Length(AD) do
begin
J := J + AD[i] + copy(BC,4 * (Length(AD) - i) +1, 4);
end;
edBinary3.Text := J;
sk := copy(J, L-LHex +1, L);
K := BinToDec(sK);
sm := copy(J, 1, L-LHex);
M := BinToDec(sM);
//CheckBoxes := CheckBoxs1 + (CheckBoxs2 shl 16);
N := K + (M shl 16);
edDec3.Text := IntToStr(N);
edHex3.Text := WordToSN(CheckBoxs1,CheckBoxs2);
// 4. All Altogether, NON backward compatible
edBinary4.Text := BinaryStr;
CheckBoxes := CheckBoxs1 + (CheckBoxs2 shl 16);
edDec4.Text := IntToStr(CheckBoxes);
edHex4.Text := LongWordToSN(CheckBoxes, ChrsX);
// 5. 64 chars
edHex5.Text := LongWordToSN(CheckBoxes, Charz);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
cb15Click(cbREset);
end;
end.