Re: Pack a Float into Bytes [message #76433] |
Thu, 02 June 2011 11:45 |
Kenneth P. Bowman
Messages: 585 Registered: May 2000
|
Senior Member |
|
|
In article <MPG.28514fa26bc10c4d9898fd@news.giganews.com>,
David Fanning <news@idlcoyote.com> wrote:
> Folks,
>
> Does anyone know off-hand how you would pack a floating point
> value into four bytes of information and then read it back
> again? (Actually, I think I know how to use the FLOAT function
> on a byte array to read the data back into a float, but
> I don't readily recall how to pack the float into the byte
> array to begin with. And I have no time this morning to
> puzzle it out.)
>
> Thanks!
>
> Cheers,
>
> David
Can you elaborate? A FLOAT is 4 bytes of data.
You can just write it unformatted. Is that what you mean?
Ken
|
|
|
Re: Pack a Float into Bytes [message #76437 is a reply to message #76433] |
Thu, 02 June 2011 09:14  |
David Fanning
Messages: 11724 Registered: August 2001
|
Senior Member |
|
|
David Fanning writes:
> Wayne, could I do the packing on a little endian
> machine and expect the byte array to be unpacked
> properly on a big endian machine? Or, would I have
> to flip the bytes in this case?
Never mind. Craig just answered my question before
I could even get it off my lips. The guy is amazing! ;-)
Cheers,
David
--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.idlcoyote.com/
Sepore ma de ni thui. ("Perhaps thou speakest truth.")
|
|
|
Re: Pack a Float into Bytes [message #76439 is a reply to message #76437] |
Thu, 02 June 2011 09:12  |
David Fanning
Messages: 11724 Registered: August 2001
|
Senior Member |
|
|
Wayne Landsman writes:
>
> Is this what you mean?
>
> b = byte(!pi,0,4) ;Convert !pi to a byte array
> print,float(b,0,1) ;Convert back to a floating point number
Wayne, could I do the packing on a little endian
machine and expect the byte array to be unpacked
properly on a big endian machine? Or, would I have
to flip the bytes in this case?
Cheers,
David
--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.idlcoyote.com/
Sepore ma de ni thui. ("Perhaps thou speakest truth.")
|
|
|
Re: Pack a Float into Bytes [message #76440 is a reply to message #76439] |
Thu, 02 June 2011 09:11  |
Craig Markwardt
Messages: 1869 Registered: November 1996
|
Senior Member |
|
|
On Jun 2, 11:56 am, David Fanning <n...@idlcoyote.com> wrote:
> David Fanning writes:
>>> Is this what you mean?
>
>>> b = byte(!pi,0,4) ;Convert !pi to a byte array
>>> print,float(b,0,1) ;Convert back to a floating point number
>
>> Yes, that's what I mean. I was just getting on to write
>> a note that I found what I was looking for on my own
>> web page! (Not in such a nice, pithy two lines, though.)
>
>> I think I'll write this up for the future! :-)
>
> I wrote a short article that demonstrates how to do this
> with floating point arrays, the real topic of my inquiry
> this morning.
>
> http://www.idlcoyote.com/code_tips/packfloat.php
Of course, there are caveats. If you are exchanging bytes between
different kinds of computers, there is a risk that they will interpret
the bytes differently. For data exchange, judicious use of the
BYTEORDER with /XDRTOF and /FTOXDR are recommended.
Craig
|
|
|
Re: Pack a Float into Bytes [message #76442 is a reply to message #76440] |
Thu, 02 June 2011 08:56  |
David Fanning
Messages: 11724 Registered: August 2001
|
Senior Member |
|
|
David Fanning writes:
>> Is this what you mean?
>>
>> b = byte(!pi,0,4) ;Convert !pi to a byte array
>> print,float(b,0,1) ;Convert back to a floating point number
>
> Yes, that's what I mean. I was just getting on to write
> a note that I found what I was looking for on my own
> web page! (Not in such a nice, pithy two lines, though.)
>
> I think I'll write this up for the future! :-)
I wrote a short article that demonstrates how to do this
with floating point arrays, the real topic of my inquiry
this morning.
http://www.idlcoyote.com/code_tips/packfloat.php
Cheers,
David
--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.idlcoyote.com/
Sepore ma de ni thui. ("Perhaps thou speakest truth.")
|
|
|
Re: Pack a Float into Bytes [message #76443 is a reply to message #76442] |
Thu, 02 June 2011 08:18  |
David Fanning
Messages: 11724 Registered: August 2001
|
Senior Member |
|
|
Wayne writes:
> Is this what you mean?
>
> b = byte(!pi,0,4) ;Convert !pi to a byte array
> print,float(b,0,1) ;Convert back to a floating point number
Yes, that's what I mean. I was just getting on to write
a note that I found what I was looking for on my own
web page! (Not in such a nice, pithy two lines, though.)
I think I'll write this up for the future! :-)
Cheers,
David
--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.idlcoyote.com/
Sepore ma de ni thui. ("Perhaps thou speakest truth.")
|
|
|
|