Aug 30, 2009 #1 dongli Technical User Joined Aug 30, 2009 Messages 9 Location CN Hi, I want to extract one specific byte from a 4-bytes real variable. How to do it? For example: real :: a = 42 integer(1) :: b a: 01000010001010000000000[00000000] -> b Thanks for help!
Hi, I want to extract one specific byte from a 4-bytes real variable. How to do it? For example: real :: a = 42 integer(1) :: b a: 01000010001010000000000[00000000] -> b Thanks for help!
Aug 30, 2009 1 #2 xwb Programmer Joined Jul 11, 2002 Messages 6,828 Location GB Depends on which version of Fortran you're using. F77: use equivalence Code: real a character*4 b integer c equivalence (a,b) a = 42.0 ! This depends on the endianess: you might want b(4) or b(1) c = b(4) print *, c F90 and above, use transfer function Upvote 0 Downvote
Depends on which version of Fortran you're using. F77: use equivalence Code: real a character*4 b integer c equivalence (a,b) a = 42.0 ! This depends on the endianess: you might want b(4) or b(1) c = b(4) print *, c F90 and above, use transfer function
Aug 30, 2009 Thread starter #3 dongli Technical User Joined Aug 30, 2009 Messages 9 Location CN Thanks xwb, it works! real a integer(1) b(4) b = transfer(a, b) BUT, b(1) is the lowest byte of a, and b(4) is the highest one. The order is a little strange. Upvote 0 Downvote
Thanks xwb, it works! real a integer(1) b(4) b = transfer(a, b) BUT, b(1) is the lowest byte of a, and b(4) is the highest one. The order is a little strange.
Aug 30, 2009 #4 xwb Programmer Joined Jul 11, 2002 Messages 6,828 Location GB It depends on the endianess of your underlying architecture. In an 8 byte sequence, the order can be 12 34 56 78 43 21 87 65 21 43 65 87 <-- this normally happpens on 16 bit machines It varies a lot and is not portable. It looks like your machine has the 2nd variant. Upvote 0 Downvote
It depends on the endianess of your underlying architecture. In an 8 byte sequence, the order can be 12 34 56 78 43 21 87 65 21 43 65 87 <-- this normally happpens on 16 bit machines It varies a lot and is not portable. It looks like your machine has the 2nd variant.
Aug 30, 2009 Thread starter #5 dongli Technical User Joined Aug 30, 2009 Messages 9 Location CN Nice! I see. Thanks a lot! Upvote 0 Downvote