write 1 byte unsigned integer to big endian binary file?

write 1 byte unsigned integer to big endian binary file?

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

5 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

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.

Steve - Intel Developer Support

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.

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".

Steve - Intel Developer Support

Excellent!
Thanks Steve.

Leave a Comment

Please sign in to add a comment. Not a member? Join today