Recent

Author Topic: Can you make this simple program better?  (Read 901 times)

EganSolo

  • Sr. Member
  • ****
  • Posts: 290
Can you make this simple program better?
« on: December 18, 2023, 01:33:03 am »
The program below works. It gets the job done. I'm thinking there may be a more efficient way (time not space) to do the same.

The program has two functions: the first takes an array of bytes and turns it into a string. The other takes the produced string and turns it back into an array of bytes. There's nothing magical about these functions. They are easy to understand and teach to someone learning how to code. Is there a straightforward, obvious way to improve on this code?

Code: Pascal  [Select][+][-]
  1. program Project1;
  2. uses SysUtils, StrUtils;
  3.  
  4. Type
  5.   ByteArray = Array of Byte;
  6.  
  7. function ByteArrayToStr(const BA: ByteArray): String;
  8. var i : integer;
  9. begin
  10.   Result := '';
  11.   If BA = nil then exit;
  12.   for i := low(BA) to high(BA) do
  13.   begin
  14.     Result := Result + IntToHex(Ba[i]);
  15.   end;
  16. end;
  17.  
  18. function StrToByteArray(const S: String): ByteArray;
  19. var i     : integer;
  20.     aCount: integer;
  21.     aStr : String;
  22. begin
  23.   If S = '' then result := nil
  24.   else begin
  25.     aCount := length(S) div 2;
  26.     SetlengtH(Result, aCount);
  27.     for i := 0 to aCount - 1 do
  28.     begin
  29.       aStr := Copy(S,i*2+1,2);
  30.       Result[i] := Hex2Dec(aStr);
  31.     end;
  32.   end;
  33. end;
  34.  
  35. var BA : ByteArray;
  36.     S  : String   ;
  37.     i  : integer  ;
  38. begin
  39.   BA := nil;
  40.   SetLengtH(BA,7);
  41.   S := ByteArrayToStr(BA);
  42.   Writeln(S);
  43.   BA := nil;
  44.   BA := StrToByteArray(S);
  45.   For i := low(BA) to high(Ba) do
  46.      writeln(i+1,' ', BA[i]);
  47.   Writeln;
  48.   Writeln('hit key when ready.');
  49.   BA := nil;
  50.   readln;
  51. end.
  52.                    
  53.  

jamie

  • Hero Member
  • *****
  • Posts: 6787
Re: Can you make this simple program better?
« Reply #1 on: December 18, 2023, 03:14:48 am »
it would be more efficient if you were to first set the size of the string to the total length before filling it.

The size of the array * two for length of the result string.

From there, you just index into the string at every odd slot to start the next group pair of HEX.

The only true wisdom is knowing you know nothing

EganSolo

  • Sr. Member
  • ****
  • Posts: 290
Re: Can you make this simple program better?
« Reply #2 on: December 18, 2023, 04:58:02 am »
Thanks, Jamie, for the good suggestion.
Here's the modified code.
Code: Pascal  [Select][+][-]
  1. program Project1;
  2. uses SysUtils, StrUtils;
  3.  
  4. Type
  5.   ByteArray = Array of Byte;
  6.  
  7. function ByteArrayToStr(const BA: ByteArray): String;
  8. var i : integer;
  9.     S : string ;
  10. begin
  11.   Result := '';
  12.   If Length(BA) = 0 then exit;
  13.   SetLength(Result,Length(BA) shl 1);
  14.   If BA = nil then exit;
  15.   for i := low(BA) to high(BA) do
  16.   begin
  17.     S := IntToHex(Ba[i]);
  18.     Move(S[1],Result[1 + i shl 1],2);
  19.   end;
  20. end;
  21.  
  22. function StrToByteArray(const S: String): ByteArray;
  23. var i     : integer;
  24.     aCount: integer;
  25.     aStr : String;
  26. begin
  27.   If S = '' then result := nil
  28.   else begin
  29.     aCount := length(S) shr 1;
  30.     Setlength(Result, aCount);
  31.     for i := 0 to aCount - 1 do
  32.     begin
  33.       aStr := Copy(S,i shl 1 + 1,2);
  34.       Result[i] := Hex2Dec(aStr);
  35.     end;
  36.   end;
  37. end;
  38.  
  39. var BA : ByteArray;
  40.     S  : String   ;
  41.     i  : integer  ;
  42. begin
  43.   BA := nil;
  44.   SetLengtH(BA,7);
  45.   for i := Low(Ba) to high(Ba) do Ba[i] := i+1;
  46.   S := ByteArrayToStr(BA);
  47.   Writeln(S);
  48.   BA := nil;
  49.   BA := StrToByteArray(S);
  50.   For i := low(BA) to high(Ba) do
  51.      writeln(i+1,' ', BA[i]);
  52.   Writeln;
  53.   Writeln('hit key when ready.');
  54.   BA := nil;
  55.   readln;
  56. end.
  57.  
  58.  

alpine

  • Hero Member
  • *****
  • Posts: 1316
Re: Can you make this simple program better?
« Reply #3 on: December 18, 2023, 12:40:46 pm »
Yo must give number of digits to IntToHex. Not all byte values have 2 digits.
No need to check for Nil after the  length was checked. No need ta call a procedure to transfer 2 chars.

Code: Pascal  [Select][+][-]
  1. function ByteArrayToStr(const BA: ByteArray): String;
  2. var i : integer;
  3.     S : string ;
  4. begin
  5.   if Length(BA) < 1 then
  6.     Exit('');
  7.   SetLength(Result, Length(BA) shl 1);
  8.   for I := Low(BA) to High(BA) do
  9.   begin
  10.     S := IntToHex(Ba[i], 2);
  11.     Result[1 + i shl 1] := S[1];
  12.     Result[2 + i shl 1] := S[2];
  13.   end;
  14. end;
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

paweld

  • Hero Member
  • *****
  • Posts: 1278
Re: Can you make this simple program better?
« Reply #4 on: December 18, 2023, 01:31:37 pm »
Typically under your example can be done like this:
Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. uses
  4.   SysUtils, StrUtils;
  5.  
  6. type
  7.   ByteArray = array of Byte;
  8.  
  9.   function ByteArrayToStr(const BA: ByteArray): String;
  10.   var
  11.     i: Integer;
  12.     S: String;
  13.   begin
  14.     Result := '';
  15.     if Length(BA) = 0 then exit;
  16.     SetLength(Result, Length(BA) shl 1);
  17.     if BA = nil then exit;
  18.     for i := low(BA) to high(BA) do
  19.     begin
  20.       S := IntToHex(Ba[i]);
  21.       Move(S[1], Result[1 + i shl 1], 2);
  22.     end;
  23.   end;
  24.  
  25.   function StrToByteArray(const S: String): ByteArray;
  26.   var
  27.     i: Integer;
  28.     aCount: Integer;
  29.     aStr: String;
  30.   begin
  31.     if S = '' then Result := nil
  32.     else
  33.     begin
  34.       aCount := length(S) shr 1;
  35.       Setlength(Result, aCount);
  36.       for i := 0 to aCount - 1 do
  37.       begin
  38.         aStr := Copy(S, i shl 1 + 1, 2);
  39.         Result[i] := Hex2Dec(aStr);
  40.       end;
  41.     end;
  42.   end;
  43.  
  44.   function ByteArrayToStr_v2(const BA: ByteArray): String;
  45.   const
  46.     hex: String[16] = '0123456789ABCDEF';
  47.   var
  48.     i, l: Integer;
  49.   begin
  50.     Result := '';
  51.     SetLength(Result, Length(BA) * 2);
  52.     for i := low(BA) to high(BA) do
  53.     begin
  54.       Result[i * 2 + 1] := hex[BA[i] div 16 + 1];
  55.       Result[i * 2 + 2] := hex[BA[i] and 15 + 1];
  56.     end;
  57.   end;
  58.  
  59.   function StrToByteArray_v2(const S: String): ByteArray;
  60.   var
  61.     i, l: Integer;
  62.     st: String;
  63.   begin
  64.     l := Length(S) div 2;
  65.     SetLength(Result, l);
  66.     SetLength(st, 2);
  67.     for i := 0 to l - 1 do
  68.       Result[i] := StrToInt('$' + S[i * 2 + 1] + S[i * 2 + 2]);
  69.   end;
  70.  
  71. var
  72.   BA, barr: ByteArray;
  73.   S: String;
  74.   i, x: Integer;
  75.   cs: Int64;
  76.   count: Integer = 1000000;
  77. begin
  78.   SetLengtH(barr, 7);
  79.   for i := Low(barr) to high(barr) do
  80.     barr[i] := i + 1;
  81.   Writeln('------ org ------');
  82.   cs := GetTickCount64;
  83.   for x := 1 to count do
  84.   begin
  85.     S := ByteArrayToStr(barr);
  86.     BA := StrToByteArray(S);
  87.   end;
  88.    Writeln('org time in ms: ', GetTickCount64 - cs);
  89.   S := ByteArrayToStr(barr);
  90.   BA := StrToByteArray(S);
  91.   Writeln(S);
  92.   for i := low(BA) to high(Ba) do
  93.     writeln(i + 1, ' ', BA[i]);
  94.   Writeln('------ v2 ------');
  95.   cs := GetTickCount64;
  96.   for x := 1 to count do
  97.   begin
  98.     S := ByteArrayToStr_v2(barr);
  99.     BA := StrToByteArray_v2(S);
  100.   end;
  101.    Writeln('v2 time in ms: ', GetTickCount64 - cs);
  102.   S := ByteArrayToStr_v2(barr);
  103.   BA := StrToByteArray_v2(S);
  104.   Writeln(S);
  105.   for i := low(BA) to high(Ba) do
  106.     writeln(i + 1, ' ', BA[i]);
  107.   Writeln;
  108.   Writeln('hit key when ready.');
  109.   readln;
  110. end.
Best regards / Pozdrawiam
paweld

 

TinyPortal © 2005-2018