如何使用 CHARACTER*50 类型的参数将参数从 C# 传递到 FORTRAN?
我有以下 FORTRAN:
SUBROUTINE MYSUB(MYPARAM)
!DEC$ ATTRIBUTES DLLEXPORT::SetPaths
CHARACTER*50 MYPARAM
WRITE(6, *) MYPARAM
END SUBROUTINE
然后我在 C# 中有以下内容
class Program
{
static void Main(string[] args)
{
StringBuilder sb = new StringBuilder(50);
sb.Append(@"something");
MYSUB(sb);
Console.ReadLine();
}
[DllImport(@"myCode.dll", EntryPoint = "MYSUB")]
public static extern void MYSUB(StringBuilder input);
}
但是,我的 FORTRAN 中的 WRITE 在“某事”之后显示了一堆垃圾。看起来字符串终止符没有被遵守。帮助!
I have the following FORTRAN:
SUBROUTINE MYSUB(MYPARAM)
!DEC$ ATTRIBUTES DLLEXPORT::SetPaths
CHARACTER*50 MYPARAM
WRITE(6, *) MYPARAM
END SUBROUTINE
Then I have the following in C#
class Program
{
static void Main(string[] args)
{
StringBuilder sb = new StringBuilder(50);
sb.Append(@"something");
MYSUB(sb);
Console.ReadLine();
}
[DllImport(@"myCode.dll", EntryPoint = "MYSUB")]
public static extern void MYSUB(StringBuilder input);
}
However, the WRITE in my FORTRAN shows a bunch of junk after "something." Looks like the string terminator is not being honored. Help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
字符串是在不同语言之间交换的最棘手的数据类型。
基本的 Fortran 字符串是固定长度的,末尾用空格填充。 (Fortran 现在有可变长度的字符串,但这些字符串更难互换。)提供内在的“trim”来抑制尾随空格; “len_trim”提供减去尾随空白的长度。
C 用空字符标记字符串的结尾。
我不知道 C# 如何处理字符串——长度的内部变量?终结者??
但 Fortran 不会理解 C# 的表示形式,它只会看到声明的字符串的完整长度,在本例中包括未初始化的内存。最好的解决方案可能是在 C# 中将字符串的其余部分初始化为空白。
Strings are the trickiest datatype to interchange between different languages.
The basic Fortran string is fixed length, padded on the end with blanks. (Fortran now has variable length strings, but those would be harder to interchange.) The intrinsic "trim" is provided to suppress trailing blanks; "len_trim" to provide the length less trailing blanks.
C flags the end of a string with a null character.
I don't know how C# handles strings -- an internal variable for the length?? a terminator??
But Fortran isn't going to understand C#'s representation, it will just see the full length of the string, as declared, including, in this case, uninitialized memory. The best solution is probably to initialize the remainder of the string to blanks in C#.