View previous topic :: View next topic |
Author |
Message |
j clark
Joined: 29 Mar 2008 Posts: 20 Location: Bala Cynwyd (Pennsylvania)
|
Posted: Thu May 22, 2008 8:14 pm Post subject: C# and Fortran arrays |
|
|
Hello, I'm back with what should be a simple question.
How do you load or store C# byte[] or int[] arrays into the Fortran equivalent? For example,
OBJECT("System.Byte[]") C_ARRAY
C_ARRAY=NEW@("System.Byte[]",256)
INTEGER(KIND=1) F_ARRAY(256)
...
DO I=1,256
C_ARRAY(I-1) ??? F_ARRAY(I)
END DO
...
If I pass F_ARRAY to the C# method, I get "No .NET Method matches..."; If I try a simple assignment ("="), I get "You cannot assign...". What, then? |
|
Back to top |
|
|
Andrew
Joined: 09 Sep 2004 Posts: 232 Location: Frankfurt, Germany
|
Posted: Thu May 22, 2008 10:30 pm Post subject: |
|
|
Have you tried using System.Int32 or System.Int16? |
|
Back to top |
|
|
j clark
Joined: 29 Mar 2008 Posts: 20 Location: Bala Cynwyd (Pennsylvania)
|
Posted: Thu May 22, 2008 11:58 pm Post subject: |
|
|
Actually, Andrew,
.Byte[] is what is essential since I am using .Net.Sockets methods to communicate with the remote systems and .Byte[] is the only overload to those methods.
But thanks,
Jerry |
|
Back to top |
|
|
PaulLaidler Site Admin
Joined: 21 Feb 2005 Posts: 7924 Location: Salford, UK
|
Posted: Fri May 23, 2008 9:37 am Post subject: |
|
|
Try declaring F_ARRAY as CHARACTER. Then use ICHAR. |
|
Back to top |
|
|
Andrew
Joined: 09 Sep 2004 Posts: 232 Location: Frankfurt, Germany
|
Posted: Fri May 23, 2008 10:26 am Post subject: |
|
|
Another possible way: pass an Int32 array from the fortran
Code: |
INTEGER F_ARRAY(256)
OBJECT("ClassLibrary3.Class1") :: OBJ
ASSEMBLY_EXTERNAL(name="ClassLibrary3.Class1.TestRoutine") :: TESTROUTINE
OBJECT("System.Int32[]") C_ARRAY
OBJ = new@("ClassLibrary3.Class1")
C_ARRAY=NEW@("System.Int32[]",256)
! make sure the indexing is correct
DO I=0,255
F_ARRAY(I + 1) = I
C_ARRAY(I) = F_ARRAY(I + 1)
END DO
CALL TESTROUTINE(OBJ, C_ARRAY)
END PROGRAM
|
And then do the conversion to byte[] in the C#:
Code: |
public void TestRoutine(int[] arr)
{
byte[] ba = new byte[arr.Length];
for (int i = 0; i < arr.Length; i++)
{
ba[i] = Convert.ToByte(arr[i]);
}
}
|
|
|
Back to top |
|
|
j clark
Joined: 29 Mar 2008 Posts: 20 Location: Bala Cynwyd (Pennsylvania)
|
Posted: Fri May 23, 2008 1:40 pm Post subject: Thank you. |
|
|
Thank you, Andrew, once again.
Works perfectly, with 16-bit integers, to boot.
I'm guessing the the compiler (correctly) objects to assigning an 8-bit integer to an unsigned byte. (Sbyte[] would probably work, but that defeats the purpose.)
And Paul, as a matter of fact I had been passing CHAR strings to C# strings for testing communications, etc. but a couple of days ago I found that I was dropping the 8th bit when converting to a C# byte[] array and I can find no method in the .Convert or .Encoding classes that work. But also, thanks.
Jerry |
|
Back to top |
|
|
|