English 中文(简体)
Fortran:如何将价值255美元存储到一个字节?
原标题:Fortran: How to store value 255 into one byte?

我想从 Fortran 调用 C api 函数。 C 函数接受数字节 :

void image(const void *bitmap, int w, int h);

中连续三个字字节代表 RGB 颜色三边,并被解释为 C. 中的 untignedchar 。 我想在 Fortran 中初始化位图, 并处理C. Fortran 中当前定义的绘图。

integer*1 rgbImage(6,2)

以初始化 2x2 的图像为例, 但编译器将不接受任务

rgbImage(1,1) = 255

来获取红色颜色。我见过使用 BYTE , UNSigned*1 , Lological*1 , for one 字节 >, UNSigned*1 , > < fcode> >>> > > > > > > > > > < colts, 帮助编译了该代码, 编译该代码帮助编译该代码帮助编译该代码帮助编译该代码,但其他Fors 可能在其他编的编的编的编的编的编的编者,但

守则最好能够对不同的堡垒汇编者进行便携式。

最佳回答

我的建议是使用 CHARACTER 变量,并使用 ACHAR 设定值(和 ICHAR 在必要时转换成整数)。这应该使您得到你想要的,并且完全可移动。例如,

character, dimension(6,2) :: rgbImage

rgbImage(1,1) = achar(255)

更新后添加 : 如果您要使用 Fortran 2003 iso_ c_ 约束性材料来连接 C 常规的界面( 高推荐度推荐! ), 那么您也可以将这种 rgbImaage 数组字符设置为类型 < code> c_char , eg

character(kind=c_char), dimension(6,2) :: rgbImage
integer(kind=c_int) :: w, h

...
rgbImage(1,1) = achar(255)
...

call image(rgbImage, w, h)

中定义了该例行程序的界面

 interface
    subroutine image(img, w, h) bind(C)
       use, intrinsic :: iso_c_binding
       implicit none
       integer(kind=c_int), intent(in), value :: w, h
       character(kind=c_char) :: img(:,:)
    end subroutine image
 end interface
问题回答

一种可能适合某些情况的替代策略是将 1 字节 c int 数组传送到整数组(1) 数组, 如 iVal (:), 然后创建另一个整数组, 如 整数组(2) i val2 (: ), 然后 :

Where(iVal(:) < 0)
    iVal2(:) = -iVal(:)+127
ElseWhere
    iVal2(:) = iVal(:)
End Where

...这可以比转换成/fro字符(有时)更有效率/更清洁,而且需要较少的编码(有时)。

如果您做了很多这类事情( 将各种类型的签名等连接到堡垒), 依靠堡垒的比特内含的几条公用程序可能是值得投资的( 如 BTest () 、 iBSet () 等 ) 。





相关问题
How do I fix this Fortran text parser for Lapack within Gem5

After about an hour, I need to ask for some help. I ve downloaded the math benchmark Lapack to run within a few CPU configurations for the simulation tool Gem5, which lets you build X86 computers and ...

gfortran, DLL, underscore

I want to access some subroutines from a third party DLL. The functions use STDCALL as the calling convention. Running dumpbin /export foo.dll gives me something like: ... 7 6 ...

Getting started with a new code in an unfamiliar language

I m starting a new project in a language I m less familiar with (FORTRAN) and am in the discovery phase. Normally reading through and figuring out code is a fairly simple task, however, this code ...

assigning values to an integer array within a Fortran module

I have a module in Fortran called QFoo. It defines a type QFooType. I want to initialize all the elements of integer array is_n to 0 and want to do it within the module. Could someone help? Thank ...

WinDbg and Intel Visual Fortran

Has anyone used WinDbg to debug an Intel Visual Fortran routine? If I have the Fortran source file that crashes with an AccViol, how can I use WinDbg to determine the line that is crashing?

converting MATLAB code to Fortran [closed]

I a medical researcher with code written in MATLAB 2009b that runs very slowly because of a self-referential loop (not sure of the programming lingo here), i.e., the results of the first iteration is ...