Convert a byte array to a structure? [message #28889] |
Thu, 17 January 2002 12:10 |
mikef
Messages: 9 Registered: May 1998
|
Junior Member |
|
|
Does IDL have a simple and/or elegant way to convert a byte array
into a structure?
I could write a brute force approach like:
proc arr_to_struct_1, arr, struct, idx
ntag = N_Tags(struct)
If (ntag Eq 0) Then return ; Not a structure
If (N_Params() LT 3) Then idx = 0
For i=0, ntag-1 Do Begin
sz = Size(struct.(i), /STRUCTURE)
Case sz.TYPE_NAME Of
'STRUCT': Begin ; Recurse to convert
tmp = struct.(i)
arr_to_struct, arr, tmp, idx
struct.(i) = tmp
End
'Byte': Begin
If (main.N_DIMENSIONS Eq 0) Then Begin
struct.(i) = arr[idx]
EndIf Else Begin
struct.(i) = Byte(arr,idx,DIMENSIONS[0], $
DIMENSIONS[1],DIMENSIONS[2],DIMENSIONS[3],
DIMENSIONS[4],DIMENSIONS[5],DIMENSIONS[6],
DIMENSIONS[7])
End
idx = idx + sz.N_ELEMENTS
End
'Int': Begin
.
.
.
Else: Begin
;Some error message
End
EndCase
EndFor
end
Or I could thrash the filesystem.
proc arr_to_struct_2, arr, struct
Openw, lu, 'trash.tmp', /GET_LUN
Writeu, lu, arr
Free_Lun, lu
Openr, lu, 'trash.tmp', /GET_LUN
Readu, lu, struct
Free_Lun, lu
end
But is there a better, platform independent, way?
(Windows NT/2K, Linux, and Solaris)
--
--
Mike Fitzgibbon MRFitz@ns.arizona.edu
UofAz, LPL phone:(520)626-4791
Systems Programmer, Pr. fax: (520)621-6783
|
|
|