Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

Function int2byte or int2char

m-santos
Beginner
1,065 Views

Do anyone know how to convert an integer*4 to a byte or character*32 ?

Thanks

M.Santos

0 Kudos
9 Replies
Steven_L_Intel1
Employee
1,065 Views
I'm not exactly sure what you're looking for. A common request is how to convert an integer to a text representation of an integer - for example, 123 to a character variable containing "123". This is done in Fortran with an "internal WRITE statement", for example:

character(10) string
integer num

num = 123
write (string, '(I0)') num
print *, string
end
0 Kudos
m-santos
Beginner
1,065 Views

In fact, I have a variable (integer*4) thatrepresents a tagname.

I need this tagname as a character*32 or a Byte in order to access a dll, called from C++.

0 Kudos
jimdempseyatthecove
Honored Contributor III
1,065 Views

You could use a UNION of the integer*4 and a character*4 then copy the character part of the union to a character*32. Note, you may have to check for BigEndian/LittleEndian issues and the copy operation will insert spaces into the remainder of the character*32 as opposed to NULL bytes.

FORTRAN also has a TRANSFER Intrinsic function
Un-tested sample

integer(4) :: I4
character(len=4) :: C4
character(len=32) :: C32
character :: C1
character :: C1x4(4)
...

C4 = TRANSFER(I4, C4)
C1x4 = TRANSFER(I4,C1,4)

Or something like that.

Jim

0 Kudos
Steven_L_Intel1
Employee
1,065 Views
I don't think the UNION or TRANSFER is what you're looking for here - can you be more explicit as to what you want to appear in this character*32? When you say "Byte" do you mean a byte array?
0 Kudos
m-santos
Beginner
1,065 Views

Exact. I mean "byte array" when I say byte.

In my database I have saved a string (FC1200PV, for example)as an integer*4 variable. NowI need a "byte array" (or character*32) that represents this integer*4, that is, FC1200PV.

0 Kudos
Steven_L_Intel1
Employee
1,065 Views
It is not possible to "save a string FC1200PV as an integer*4 variable." Do you mean that you have an integer*4 variable with the value 1200 and you want to construct the string FC1200PV based on it? If so, you'd use internal WRITE as suggested above in the following manner:

character(32) tagname
integer key
key = 1200
write (tagname,'("FC",I4.4,"PV")') key

The I4.4 assumes that you always want four digits with leading zeroes. If you want no leading zeroes, then use I0 instead.
0 Kudos
m-santos
Beginner
1,065 Views

I read a binary file in C. This variable Name is defined in C as

char Name[32]

This variable is passed from C to Fortran in a structure.

In Fortran, I define this variable as integer*4 Name[32]

I do that because in C and Fortran, char and character are different.

So, I need the string that represents this integer variable in Fortran (character or byte array).

Sorry for the misunderstanding.

0 Kudos
Steven_L_Intel1
Employee
1,065 Views
Ah, then Jim had the right idea after all. Note, however, that your Fortran array is 128 characters long. Maybe you wanted Name(8) instead? You can use the TRANSFER intrinsic for this, but why not just declare it the right type in Fortran to begin with? CHARACTER(32) should work as a structure component. Note however that if not all 32 characters are used, C will typically end the string with a NULL (CHAR(0)) byte whereas Fortran wants to see blank padding. You'll have to deal with that.
0 Kudos
m-santos
Beginner
1,065 Views

Steve and Jim,

It works well with the TRANSFER function.

Thank you very much.

M.Santos

0 Kudos
Reply