### Bookstore

 Computer Math and Games in Pascal (preview) Lazarus Handbook

### Author Topic: [ SOLVED ] conversion code from c# to lazarus  (Read 815 times)

#### superc

• Full Member
• Posts: 197
##### [ SOLVED ] conversion code from c# to lazarus
« on: August 12, 2022, 12:09:24 pm »
Hello,

I must to convert a code of c# program into lazarus but I don't understand how:

Code: Pascal  [Select][+][-]
1.         public PageMem(UInt64 mBaseAddress, int size, byte[] FillPattern)
2.         {
3.             Data = new byte[size];
4.             for (int i = 0; i < Data.Length; i += FillPattern.Length)
5.             {
6.                 Array.Copy(FillPattern, 0, Data, i, Data.Length > (i + FillPattern.Length) ? FillPattern.Length : Data.Length - i);
7.             }
9.         }
10.

can someone help me,

« Last Edit: August 22, 2022, 09:47:10 am by superc »

#### Zvoni

• Hero Member
• Posts: 1539
##### Re: conversion code from c# to lazarus
« Reply #1 on: August 12, 2022, 12:38:33 pm »
Code: Pascal  [Select][+][-]
1. Type
2.   TByteArray=Array Of Byte;
3.
5. Var
6.    Data:TByteArray;
7.    i:Integer=0;
8.    L:Integer;
9. Begin
10.    SetLength(Data,size);
11.    While (i<Length(Data)) Do
12.        Begin
13.            If Length(Data)>(i+Length(FillPattern)) Then L:=Length(FillPattern) Else L:=Length(Data)-i;
14.            Move(FillPattern[0],Data[i],L);
15.            Inc(i,Length(FillPattern));
16.        End;
18. End;
What the Procedure is basically doing is filling up "Data" with (repeated) FillPattern.
Kind of Like FillChar, FillWord etc.
« Last Edit: August 12, 2022, 12:40:16 pm by Zvoni »
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

#### superc

• Full Member
• Posts: 197
##### Re: conversion code from c# to lazarus
« Reply #2 on: August 12, 2022, 12:53:50 pm »
Wooow, it will take me a while to study the code

BaseAddress is a public uint64, in fact seeing the code it seems to me that it is useless.

Thanks you very much.
« Last Edit: August 12, 2022, 01:00:54 pm by superc »

#### 440bx

• Hero Member
• Posts: 3163
##### Re: conversion code from c# to lazarus
« Reply #3 on: August 12, 2022, 01:14:45 pm »
BaseAddress is a public uint64, in fact seeing the code it seems to me that it is useless.
And hopefully so is "Data" otherwise that code leaks the entire array memory block.  ("Data" has to eventually be freed somewhere which means it has to be accessible to the method that is responsible for freeing it.)

In the code that @Zvoni gave you, "Data" is a local variable but, in _your_ code, "Data" cannot be local to the method (otherwise its value is lost and, as a result, there is no way to free the block it points to.)
FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 SP1 64bit.

#### Zvoni

• Hero Member
• Posts: 1539
##### Re: conversion code from c# to lazarus
« Reply #4 on: August 12, 2022, 03:22:52 pm »
BaseAddress is a public uint64, in fact seeing the code it seems to me that it is useless.
And hopefully so is "Data" otherwise that code leaks the entire array memory block.  ("Data" has to eventually be freed somewhere which means it has to be accessible to the method that is responsible for freeing it.)

In the code that @Zvoni gave you, "Data" is a local variable but, in _your_ code, "Data" cannot be local to the method (otherwise its value is lost and, as a result, there is no way to free the block it points to.)
Correct.
In a nutshell, i translated the C#-code as close as possible to be „literally“ the same.
Yes, i used Data as a local var (to be able to compile), but my guess is, that that procedure is a member of a class (and „this.BaseAddress“ is a strong hint), and Data is actually a Property of the class.
After i figured out, what the function is actually doing, i wrote my „conclusion below (the bit with FillChar).

And people laugh at me when i call it Dot CRAP…..
« Last Edit: August 12, 2022, 03:27:39 pm by Zvoni »
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

#### superc

• Full Member
• Posts: 197
##### Re: conversion code from c# to lazarus
« Reply #5 on: August 12, 2022, 03:28:47 pm »

In a nutshell, i translated the C#-code as close as possible to be „literally“ the same.
After i figured out, what the function is actually doing, i wrote my „conclusion below (the bit with FillChar).

And people laugh at me when i call it Dot CRAP…..

I have in fact adapted the code: I'm trying to convert the 1000 utilities made in the company where I work from dot crap to Lazarus ... I'm happy with that

#### Zvoni

• Hero Member
• Posts: 1539
##### Re: conversion code from c# to lazarus
« Reply #6 on: August 12, 2022, 03:30:32 pm »
Please look at my reply to 440bx again. I‘ve edited it a bit
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

#### BeniBela

• Hero Member
• Posts: 847
##### Re: conversion code from c# to lazarus
« Reply #7 on: August 12, 2022, 05:47:33 pm »

This might be faster:

Code: Pascal  [Select][+][-]
1. var  Data:TByteArray;
2.
4. Var
5. //   Data:TByteArray;
6.    L: SizeInt;
7. Begin
8.    SetLength(Data,size);
9.    L := length(FillPattern);
10.    if L > size then L := size;
11.    move(FillPattern[0], data[0], L);
12.    while 2 * L < size do begin
13.      move(Data[0], data[L], L);
14.      L := L * 2;
15.    end;
16.    move(data[0], Data[L], size - L);
18. End;
19.

And hopefully so is "Data" otherwise that code leaks the entire array memory block.  ("Data" has to eventually be freed somewhere which means it has to be accessible to the method that is responsible for freeing it.)

It is a managed array

I have in fact adapted the code: I'm trying to convert the 1000 utilities made in the company where I work from dot crap to Lazarus ... I'm happy with that

You are doing it wrong. You should convert the Lazacrap to dot crap to make it _modern_
« Last Edit: August 12, 2022, 06:09:00 pm by BeniBela »

#### superc

• Full Member
• Posts: 197
##### Re: conversion code from c# to lazarus
« Reply #8 on: August 12, 2022, 06:34:28 pm »

You are doing it wrong. You should convert the Lazacrap to dot crap to make it _modern_

Thanks, did you notice that you are on the lazarus forum?

#### dseligo

• Hero Member
• Posts: 814
##### Re: conversion code from c# to lazarus
« Reply #9 on: August 13, 2022, 12:05:56 am »

You are doing it wrong. You should convert the Lazacrap to dot crap to make it _modern_

Thanks, did you notice that you are on the lazarus forum?

BeniBela was probably sarcastic

#### superc

• Full Member
• Posts: 197
##### Re: conversion code from c# to lazarus
« Reply #10 on: August 13, 2022, 09:46:12 am »

You are doing it wrong. You should convert the Lazacrap to dot crap to make it _modern_

Thanks, did you notice that you are on the lazarus forum?

BeniBela was probably sarcastic

me too, __