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

write 1 byte unsigned integer to big endian binary file?

AndyM
Beginner
778 Views
I have 2 byte positive integers which I need to write, as 1 byte unsigned integers, to a binary file.
The binary file is opened as big endian.
Is there a simple way to do this?

Thanks, Andy
0 Kudos
4 Replies
Steven_L_Intel1
Employee
778 Views
I don't understand what you want to do. Can you provide examples with, perhaps, hex strings or bit patterns explaining what you want to go where? Big-endian has no meaning for one-byte values.
0 Kudos
AndyM
Beginner
778 Views
I want to write integers ranging from 0 (as hex 00) through 255 (as FF).
(I wanted to make clear that big endian was in play in case that might affect the strategy.)

With signed integers I must use 2 bytes to get the 0 to 255 range. If I write the following array I get 16 binary bytes. I need to write it as 8 bytes. Integer(1) doesn't let me count to 255.

integer(2) IQ_2B(2,4)
write(fileID) IQ_2B(1,:), IQ_2B(2,:)

I thought writing one integer at a time might simplify the problem, thus my One Byte question.


Looks like an easy work around is to add

integer(1) IQ_1B(2,4)

! copy:
IQ_1B = IQ_2B

! and
write(fileID) IQ_1B(1,:), IQ_1B(2,:) ! instead of writing IQ_2B
! letting the signed integer hex representation do the job for me.
0 Kudos
Steven_L_Intel1
Employee
778 Views
How about:

write (fileID) INT(IQ_2B(1:),1), INT(IQ_2B(2,:),1)

Since our compiler does not offer integer overflow detection, there should be no problem using values in the "unsigned range".
0 Kudos
AndyM
Beginner
778 Views
Excellent!
Thanks Steve.
0 Kudos
Reply