Solved

Binary to Text, Text to Binary - Part II

Posted on 2010-09-14
3
711 Views
Last Modified: 2012-08-14
I would like to refine some work i had help on

see: http://www.experts-exchange.com/Programming/Languages/Pascal/Delphi/Q_26470056.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 *************************

Open in new window

0
Comment
Question by:Looking_4_Answers
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 25

Accepted Solution

by:
epasquier earned 500 total points
ID: 33676896
Try to convert BinToString with ThrowException = True.
If the source is not compatible with Binary representation of some text, then it will raise an exception.
Trap that exception, and in this case you convert the string to binary, which always succeed.
procedure TfrmConversionsBinary.btnConvertBinarytoTextClick(Sender: TObject);
begin
 try
  memOne.Lines.Text:= BinToString(memOne.Lines.Text, True);
 except
  memOne.Lines.Text:= StringToBin(memOne.Lines.Text);
 end; 
end;

Open in new window

0
 
LVL 3

Expert Comment

by:NevTon
ID: 33678115
Hi,

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.

function ToBins(AText: String): String;
var
  i: Integer;

  function dectobin(dec: Byte): String;
  var
    j: Byte;
  begin
    Result:='';
    for j:=0 to 7 do begin
      if (dec shr (7 - j) and 1) = 1 then
        Result:=Result + '1'
      else
        Result:=Result + '0';
    end;
  end;

begin
  Result:='';
  for i:=1 to Length(AText) do begin
    Result:=Result + dectobin(ord(AText[i]));
  end;
end;

function ToText(ABins: String): String;
var
  i: Integer;

  function bintodec(bin: String): Byte;
  var
    j: Byte;
  begin
    Result:=0;
    if Length(bin) < 8 then
      Exit;
    for j:=1 to 8 do begin
      if bin[j] = '1' then
        Result:=Result + (1 shl (8 - j));
    end;
  end;

begin
  Result:='';
  for i:=1 to (Length(ABins) div 8) do begin
    Result:=Result + chr(bintodec(Copy(ABins, 1, 8)));
    Delete(ABins, 1, 8);
  end;
end;

function IsBins(AText: String): Boolean;
var
  i: Integer;
begin
  Result:=True;
  for i:=1 to Length(AText) do begin
    if not (AText[i] in ['0', '1']) then
      Result:=False;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
 if IsBins(Memo1.Lines.Text) then
   Memo1.Lines.Text:=ToText(Memo1.Lines.Text)
 else
   Memo1.Lines.Text:=ToBins(Memo1.Lines.Text);
end;

Open in new window

0
 
LVL 25

Expert Comment

by:epasquier
ID: 33690064
Hi Looking_4_Answers !

Have you tried the solution I provided ?
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Delphi Spellcheck in Webbrowser 1 51
QRReport  TQrmemo vertical stretching 1 57
Delphi: making a BW image transparent 10 94
delphi popmenu non latine charcters 3 35
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question