INTERNAL: Convert 4 (four) bytes to 1 (one) float
Topic:
This code shows how to convert an array of N bytes into an array of N/4 floats.
Discussion:
A float is a 32 bit number with values in the range ± 10^38 as all the cool kids know. It is stored in your machine as 4 bytes (8 bits each), again nothing new.
But suppose you have an array of bytes and you know that each set of 4 bytes represents a float. And further suppose you want to combine those into float type data. Normally this would be done with the OPENR and READU routines directly from a binary data file, which works very nicely. But suppose you want to programmatically convert a byte array of size N, that your program has created, into an array of N/4 floats without first writing to a binary file and then reading it back in with OPENR and READU. Well my friends, the answer is here.
The code below shows how to do just that. It will create an array of N floats from 0 to N-1 and rip them apart into their component bytes. It will then recombine them into the floats that they came from, this second part is the intended demonstration. As you will see from the print statements, the program will print out both the float, and it's component bytes, both before and after. This will give you an idea of how to do this.
***Note: This has been updated from a previous version. The previous version didn't take into account byte-order and would only work for little-endian machines. This new version solves that problem. You will notice now, that the bytes will apear in opposite order if run on big-endian vs little-endian, but that the floats deconstructed and reconstructed will always be the same.
Also take care when importing from a binary data file, you may then need to do a byte-swap operation depending on the platform the data were created on. In that case I would suggest using OPENR and READU and potentially a SWAP_ENDIAN instead of the approach below.
The approach listed below is really only useful if you are creating a byte array in-program and wish to then combine them into floating point values.
|
Solution:
pro float_from_bytes_ex
;set n to the number of floats we want to rip apart and
;put back together
n=5
;First lets create some 32bit float values to work with.
;Each float is composed of 4 bytes of data
flts = findgen(n)
;Then rip them apart into their component bytes and place them
;into a byte array, which will have 4 times as many elements as
;the float array.
byts = bytarr(n*4)
off=0
print, '------------------>Begin deconstructing floats'
for ii=0, n-1 do begin
print, ''
print, 'Float value: ' + string(flts[ii])
byts[ii*4] = fix(flts[ii], 0, 4, type=1)
print, 'Component Bytes that make up the float:'
for jj=ii*4, ii*4+3 do begin
print, byts[jj], format='(b08)'
endfor
endfor
print, '------------------>End deconstructing floats'
;Now we want reconstruct the original float values from the
;component bytes. Take note of the offset parameter to the fix() function
off=0
newflts = fltarr(n)
print, ''
print, '------------------>Begin recombining floats'
for mm=0, n-1 do begin
newflts[mm] = fix(byts, off, type=4)
print, ''
print, 'Float constructed:' + string(newflts[mm])
print, 'From these bytes:'
for jj=mm*4, mm*4+3 do begin
print, byts[jj], format='(b08)'
endfor
off = off + 4
endfor
print, '------------------>End recombining floats'
end