1

I have done some Example to convert a string to binary but i couldn't find a way to walk on each character in the string and complete the whole calculations process and then step to the next character in the string, Here is my code:

var i,j, rest, results :integer;
    restResult : string;
begin
    results := 1;
    for i := 1 to length(stringValue) do
      begin
        while (results > 0) do
          begin
            results    := ord(stringValue[i]) div 2;
            rest       := ord(stringValue[i]) mod 2;
            restResult := restResult + inttostr(rest);
          end;
      end;
// Get The Rests Backwards
    for i := length(restResult) downto 1 do
          begin
              result := result + restResult[i];
          end;

The application always get into infinite loop, any suggestions?

halocedark
  • 97
  • 1
  • 8
  • 1
    Yes, it is entirely expected that this code will lead to an infinite loop. I can see that by reading it. I understand that a novice programmer might not be able to see it from simple reading of the code. So, use your debugger to explore execution of the code. Then you will understand why there is an infinite loop. As for what to do to the code, that depends on what the goal of the program actually is. Not at all clear. – David Heffernan Nov 13 '18 at 07:26

1 Answers1

1

Your results := ord(stringValue[i]) div 2; remains the same, because stringValue[i] does not change, so while loop is infinite.

To solve this mistake:

  for i := 1 to length(stringValue) do
  begin
      t := ord(stringValue[i]);
      repeat
         restResult := restResult + inttostr(t mod 2);
         t := t div 2; 
      until t = 0;
  end;

But note that you cannot divide resulting string into pieces for distinct chars, because length of binary representation will vary depending on char itself.

This is example of code with fixed length for representation of char (here AnsiChar):

function AnsiStringToBinaryString(const s: AnsiString): String;
const
  SBits: array[0..1] of string = ('0', '1');
var
  i, k, t: Integer;
  schar: string;
begin
  Result := '';
  for i := 1 to Length(s) do begin
    t := Ord(s[i]);
    schar := '';
    for k := 1 to 8 * SizeOf(AnsiChar) do begin
      schar := SBits[t mod 2] + schar;
      t := t div 2
    end;
    Result := Result + schar;
  end;
end;

'@A z': (division bars are mine)
01000000|01000001|00100000|01111010
   @       A        space     z 
MBo
  • 72,080
  • 5
  • 47
  • 77
  • This isn't going to yield anything very useful. Different input strings will lead to identical outputs which is surely not intentional. – David Heffernan Nov 13 '18 at 07:44
  • Yes, I've already described this problem and proposed solution – MBo Nov 13 '18 at 07:46
  • FWIW, the original while loop will eventually crash, when RestResult has consumed all memory. – Rudy Velthuis Nov 13 '18 at 07:46
  • It worked but, does "AnsiChar" changes value while looping through each time? because now it's Equal To "1", and why you used "8" in the loop header? – halocedark Nov 13 '18 at 09:59
  • No, it is type name, here is determination of char bit size – MBo Nov 13 '18 at 10:01
  • is it required to define array of string "sBits", can't i just assign to a simple string variable? – halocedark Nov 13 '18 at 10:04
  • @halocedark You can do whatever you like, it's your program! I'd start by trying to understand what this code does, and why your code failed. I'd also suggest that you learn to use the debugger so that you will be able to track down mistakes in the future, and gain an understanding into why code behaves as it does. – David Heffernan Nov 13 '18 at 10:05
  • You can use just `for k := 1 to 8 do` but I wanted to emphasize bit size of treated data elements – MBo Nov 13 '18 at 10:11
  • I just want to make a string to binary converter like the ones in the web, trying to get the idea together using the code above, also i know where the problems comes from in my code, but the idea for getting me to the final result is missing, that's all :) – halocedark Nov 13 '18 at 10:12
  • thank you so much @MBo, you awesome :), Thank you all :) – halocedark Nov 13 '18 at 10:12