[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Pascal to C#

Posted on 2006-10-19
2
Medium Priority
?
218 Views
Last Modified: 2010-04-05
Need a conversion to C# again.. optimized function of last request.

Ciuly around? :-)

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

begin
 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
 end;

'a' again is a byte array.
0
Comment
Question by:valvet
[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 Comments
 
LVL 28

Accepted Solution

by:
2266180 earned 2000 total points
ID: 17768469
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;
                              i++;
                              s2 = s2.Substring(p+1);// 1=length(mark). it's always 1 since it's a char
                        }
                        else
                        {
                              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.
0
 

Author Comment

by:valvet
ID: 17768572
Excellent as usual :-).

Cheers!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

650 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