Hello all
..This will be a dumping ground for porting C/C++ code snips to Delphi..
And is a work-in-progress
From this point onward, I will put ONLY those code snips that have
been resolved with working port examples.
I'm working on a project for someone, and it requires porting C# code to
Delphi 6 and I'm having trouble with a few syntax and other coding attributes.
I'm not very knowledgeable with C (or any variation) and some things
just don't make sense to me, hence my trouble porting to Delphi.
So, I thought I'd start a C# to Delphi topic, in hopes that I find
the answers here, where we all gather and share knowledge. Anyways.
So, here goes some first time C# code nips for you to help me in
understanding and porting.
It looks to me the C# code below is setup to take what's in A, (a TWO
DIMENTIOANL ARRAY of byte) (assuming "A[x,x] of bytes" but of UNKNOWN
size) to be further processed in this function.
It also seems to me, that after processed in this myFuncTION function,
that the return values will be a 2 dimentional array, but of Double. ie,
myFuncTION[x,x].
So, the size for myFuncTION could be, for instance, (1,1, 1,2, 1,3) or even
(1,1, 1,2) .. or whatever size. I don't know, because the code below
doesnt indicate. It's an undetermined size, legal in C# code. I think
that Delphi has this also, but the syntax eludes me, and this is a two
dimenational array. The twist to this, (to me) is that the return
value is going to B, but which seems to be a 2 dimentional array of
double. And to add more confusion, how is myFuncTION getting it's values
filled (or returned) w/ data, if its going to B (or, B[x,x],[x,x]) ??
However, B is of known size. Its a 2 dimention of 2 rows, 2 columns.
Given C#
The closest I've come to matching the above function, is:Code:public double[,] myFuncTION (byte[,] A) { double TIONa, TIONb; int i,j; double[,] B = new double[2,2]; . . return B; }
Code:Type T_myfunction = array of array of byte; Function myFuncTION(const a : T_myfunction): array of array of double; var TIONa, TIONb: double; i, j: integer; // double[,] B = new double[2,2]; B: array[1..2, 1..2] of Double; begin for i := 1 to 2 do begin ... for j:= 1 to 2 do begin ... end; end; result := B; end;
Then, here's another one that's confusing to me. My C++
bible say's that "*=" means Multiplication Assignment
Given C#
..the equivalent in Delphi is definately unknown at this time:Code:B(i,j) *= (i * 0.1 * TIONa * TIONb * j)
Code://
And then here's another bummer code snip:
Given C#
Code:B[s,t] = (INT) math.round((num1 / num2) - (BYTE) (n[s,t]));
And last..
But I think I figured this one out.. if someone can double-verify
for me
Given C#
..I believe the equivalent in Delphi is:Code:t >>= 1;
Thank you for any assistance,Code:t := t shr 1; // shif bit right
-vhelp 3151
+ Reply to Thread
Results 1 to 12 of 12
-
-
Originally Posted by vhelp
Code:B(i,j)=B(i,j) * (i * 0.1 * TIONa * TIONb * j)
-
TGIF everyone
Thanks Mats.
That answer definately helps me out a lot
Now, if someone could answer some of the other ones above, that'll help
me out even more so.
Thanks again,
-vhelp 3152 -
Its been long long time since I left C programming. But t>>l is indeed equivalent to shifting bits to the right. Which is equivalent to the number being divided by 2 or multiple of 2 depending on the bits shifted. Similarly shifting to right is equivalent to multiplying the number.
When I was born I was so shocked that I could'nt speak for 18 months. -
Yes.
Code:t := t shr 1; // shif bit right
T >>= 1
Delphi/Pascal:
* shr = shift right bit
* shl = Shift left bit
I came to that knowledge, when I found it in my "Standard C++ Bible"
book, and guested at it
But, not everything is covered. For instance, like what Mat brought up
with the answer to my other C to Pascal code snip:
Code:B(i,j) *= (i * 0.1 * TIONa * TIONb * j)
B(i,j) = B(i,j) * (i * 0.1 * TIONa * TIONb * j)
However, then I came across this, though similar to above, but notice
instead of the "*=", there is a "+=" (see the PLUS sign)
Code:B(i,j) += (i * 0.1 * TIONa * TIONb * j)
B(i,j) = B(i,j) + (i * 0.1 * TIONa * TIONb * j)
I'm wondering if I'm correct?? ??
Thanks.
-vhelp 3239 -
And, here is yet another (on-going) question, if you or anyone else know
the answer to. Given the code snip below:
Code:01: public double[,] calculateVALUES (byte[,] A) 02: { 03: double[,] BValues = new double[8,8]; 04: 05: for (r=0; r<8; r++) 06: for (c=0; c<8; c++) 07: BValues[r,c] += A[r,c] * math.pi 08: 09: return A 10: }
* LN 01 - the user is feeding in an array matrix of [8,8] values of Bytes.
* LN 03 - is assigning an variable [8,8] array values of Double to BValues.
* LN 05-07 is calculating the values to fill in to BValues[8,8] array.
* LN 05-07 is taking the VAR param from 'byte[,] A' and calculating those
values with the '* math.pi' C++ math function, and storing them in BValues[8,8].
* LN 09 - is returning the values calculated in BValues[8,8], into A[8,8].
And, here's what I think the Delphi/Pascal equivalent is:
Code:01: Type 02: byteARRAY = array[1..8, 1..8] of Byte; 03: doubleARRAY = array[1..8, 1..8] of Double; 04: . 05: . 06: Function calculateVALUES(A: byteARRAY) : doubleARRAY; 07: var 08: r,c: integer; 09: BValues: doubleARRAY; 10: begin 11: 12: for r := 1 to 8 do 13: for c := 1 to 8 do 14: BValues[r,c] := BValues[r,c] + A[r,c] * math.pi 15: 16: result := BValues 17: 18: end;
( or C# ) code.
Can anyone validate the above to be correct, for me ??
Thanks.
-vhelp 3240 -
The assignment statement inside the for loop is wrong. It should be like
BValues[r,c] := BValues[r,c] + A[r,c] * math.piWhen I was born I was so shocked that I could'nt speak for 18 months. -
Originally Posted by vhelp
/Mats -
@ sanjayk
Thanks. I can't believe I missed that.., pretty ironic though.
@ mats.
I cought an error in my code post above, and I fixed it (but you'r
quote, is showing the error) - fwiw
Thank you both for your help on everything so far.
-vhelp 3242 -
Originally Posted by mats.hogberg
B(i,j) = B(i,j) * (i * 0.1 * TIONa * TIONb * j)
Lesson learned:
Never debug read code when you don't have unlimited time to throw at it!
/Mats -
Hello again all
I have another porting job. Below is a code snip from a DCT example
written in C code, and is awaiting a port to Delphi. I'm working on
this now, but I thought I would post what I have. I think I have it,
but I need to finish to be sure. The secret (to this type of delphi
port) will be unleashed soon, I hope.
I will post on the main page ( pg. 1 ) should I have a working port.
const
N = 8;
struct zigzag {
int row;
int col;
} ZigZag[ N * N ] =
{
{0, 0},
{0, 1}, {1, 0},
{2, 0}, {1, 1}, {0, 2},
{0, 3}, {1, 2}, {2, 1}, {3, 0},
{4, 0}, {3, 1}, {2, 2}, {1, 3}, {0, 4},
{0, 5}, {1, 4}, {2, 3}, {3, 2}, {4, 1}, {5, 0},
{6, 0}, {5, 1}, {4, 2}, {3, 3}, {2, 4}, {1, 5}, {0, 6},
{0, 7}, {1, 6}, {2, 5}, {3, 4}, {4, 3}, {5, 2}, {6, 1}, {7, 0},
{7, 1}, {6, 2}, {5, 3}, {4, 4}, {3, 5}, {2, 6}, {1, 7},
{2, 7}, {3, 6}, {4, 5}, {5, 4}, {6, 3}, {7, 2},
{7, 3}, {6, 4}, {5, 5}, {4, 6}, {3, 7},
{4, 7}, {5, 6}, {6, 5}, {7, 4},
{7, 5}, {6, 6}, {5, 7},
{6, 7}, {7, 6},
{7, 7}
};
-vhelp 3409 -
Ok. I finally got it.., after about 100 variations of trial 'n error scenarios,
I finally snagged it.
Yea.
For dificulty in char position and space reason, I am demo'ing an image
as a representation of my final port. If anyone has a better way of demo'ing
source code on this forum, please advise. Thanks.
-vhelp 3410
Similar Threads
-
FAQ -Common playing problems!
By Baldrick in forum Software PlayingReplies: 8Last Post: 28th Jul 2014, 05:15 -
FAQ didn't answer my question. What's the best DVD-R 9GB media to buy?
By insomnia976 in forum MediaReplies: 7Last Post: 18th Jul 2009, 13:10 -
new to delphi...
By sandralink in forum ProgrammingReplies: 1Last Post: 17th Feb 2008, 19:28 -
DV FAQ - Read me first
By Baldrick in forum Camcorders (DV/HDV/AVCHD/HD)Replies: 10Last Post: 11th Jan 2008, 16:40 -
Porting a linux app to a win32 binary
By freebird73717 in forum ProgrammingReplies: 3Last Post: 25th Oct 2007, 13:40