Pascal to C#

valvet used Ask the Experts™
Need a conversion to C# again.. optimized function of last request.

Ciuly around? :-)

 i, p: integer;
 b, d: byte;
 s1, s2: string;
 mark = chr($4f);

 s2 := strloadfromfile('File.bin');// get data
 s2 := Copy(s2,$1d0, Length(s2)); //skip first 1d0 bytes
 i := 0; // init loop variable
 while i<64 do begin //until all array is filled
  p := pos(mark,s2);// get position of mark
  if p>0 then begin // marker word found, get id
   b := ord(s2[p-1]); // array item
   // ord - return numerical value of char
   if (ord(s2[p-4])=$2c)and(ord(s2[p-3])=$10) then dec(b,3); // check if need to dec item
   a[b] := i; // save item
   inc(i); // shift array index
   s2 := Copy(s2, pos(mark,s2)+length(mark), Length(s2));// copy rest of data
  end else i := 64;//end of parsing

'a' again is a byte array.
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2007
I presume strloadfromfile is one of the functions converted already.

            public byte[] optimized()
                  string s2 = strLoadFromFile("test.bin");
                  s2 = s2.Substring(0x1d0);
                  int p;
                  byte i = 0;
                  int b;
                  byte[] a = new byte[64];
                  while (i<64)
                        p = s2.IndexOf(mark);
                        if (p>-1)
                              b = (byte)s2[p-1];
                              if (s2[p-4] == 0x2c && s2[p-3] == 0x10)
                                    b = b - 3;
                              a[b] = i;
                              s2 = s2.Substring(p+1);// 1=length(mark). it's always 1 since it's a char
                              i = 64;
                  return a;

btw, the delphi version is not that optimized. copy function will always reallocate the string buffer. so it's better to use posex from strutils unit and work with an index into s2. in C# that would translate to s2.indexOf(mark,the_index);
also note that 1=length(mark). it's always 1 since it's a char. of course if you intend to use a string as a mark, then ignore my optimization and replace that with mark.Length and declare mark as string rather than char.


Excellent as usual :-).


Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial