Looking_4_Answers
asked on
Binary to Text, Text to Binary - Part II
I would like to refine some work i had help on
see: https://www.experts-exchange.com/questions/26470056/Binary-to-Ascii-Text-and-Visa-Versa.html
I have two memos and two convert buttons
Convert Binary to text button (Converts Binary in memOne and displays text in memTwo)
Convert Text to Binary button (Converts text in memOne and displays binary in memTwo)
see code below (by EE expert - epasquier)
I would like to have one Tmemo, and one button (convert)
Is this possible
When the convert button is clicked, it checks the Tmemo and determines if the memo is all zeros and ones and then converts to words (text), else it checks to see that its just words (text) and then converts to binary
thanks
see: https://www.experts-exchange.com/questions/26470056/Binary-to-Ascii-Text-and-Visa-Versa.html
I have two memos and two convert buttons
Convert Binary to text button (Converts Binary in memOne and displays text in memTwo)
Convert Text to Binary button (Converts text in memOne and displays binary in memTwo)
see code below (by EE expert - epasquier)
I would like to have one Tmemo, and one button (convert)
Is this possible
When the convert button is clicked, it checks the Tmemo and determines if the memo is all zeros and ones and then converts to words (text), else it checks to see that its just words (text) and then converts to binary
thanks
procedure TfrmConversionsBinary.btnConvertBinarytoTextClick(Sender: TObject);
begin
memTwo.Lines.Text:= BinToString(memOne.Lines.Text, True);
end;
procedure TfrmConversionsBinary.btnConvertTexttoBinaryClick(Sender: TObject);
begin
memTwo.Lines.Text:= StringToBin(memOne.Lines.Text);
end;
//******************** BINARY *************************
function BinToInt(Bin:String;ThrowException:Boolean=False):Cardinal;
Var
i,L:integer;
begin
Result:=0;
i:=1;
L:=Length(Bin);
While i<=L do
begin
Result:=Result SHL 1;
if Bin[i]='1'
Then Inc(Result)
Else if ThrowException And (Bin[i]<>'0')
Then Raise Exception.Create('Invalid binary digit in ['+Bin+']');
Inc(i);
end;
end;
function IntToBin(C:Cardinal;Digits:Byte=8):String;
Var
N:Byte;
Const
Bits='01';
begin
Result:='';
N:=0;
While (C>0) Or (N=0) Or ((N Mod Digits)>0) do
begin
Result:=Bits[(C And 1)+1]+Result;
C:=C SHR 1;
Inc(N);
end;
end;
function StringToBin(S:String):String;
Var
i:integer;
begin
Result:='';
for i:=1 to Length(S) do Result:=Result+IntToBin(Ord(S[i]));
end;
function BinToString(S:String;ThrowException:Boolean=False):String;
Var
N:Integer;
begin
Result:='';
N:=Length(S);
if ThrowException And ((N Mod 8)>0) Then Raise Exception.Create('Number of binary digits must be a multiple of 8');
While N>0 do
begin
Result:=Result+Char(BinToInt(Copy(S,1,8),ThrowException));
S:=Copy(S,9,N);
N:=N-8;
end;
end;
//******************** END BINARY *************************
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Hi Looking_4_Answers !
Have you tried the solution I provided ?
Have you tried the solution I provided ?
Better way is to examine the memo in a loop (I hope that this text is not very large) for having something else than zeros and ones.
This always work.
If in this memo is only 0 and 1 convert to text else this is text, convert it to bins.
Open in new window