1 | ;+ |
---|
2 | ; |
---|
3 | ; @file_comments |
---|
4 | ; Returns the binary representation of a number of any numerical type. |
---|
5 | ; |
---|
6 | ; @param NUMBER {in}{required} |
---|
7 | ; scalar or array of numbers (any numerical type) |
---|
8 | ; |
---|
9 | ; @returns |
---|
10 | ; Byte array with binary representation of numbers. |
---|
11 | ; |
---|
12 | ; @examples |
---|
13 | ; Binary representation of 11b: |
---|
14 | ; IDL> print, binary(11b) |
---|
15 | ; 0 0 0 0 1 0 1 1 |
---|
16 | ; Binary representation of pi (x86: Little-endian IEEE representation): |
---|
17 | ; IDL> print, format='(z9.8,5x,4(1x,8i1))', long(!pi,0), binary(!pi) |
---|
18 | ; 40490fdb 01000000 01001001 00001111 11011011 (x86 Linux) |
---|
19 | ; 0fdb4149 00001111 11011011 01000001 01001001 (Alpha OpenVMS) |
---|
20 | ; IDL> print, format='(8(1x,8i0))', binary(!dpi) |
---|
21 | ; 01000000 00001001 00100001 11111011 01010100 01000100 00101101 00011000 |
---|
22 | ; Some first tests before type double was added: |
---|
23 | ; print, format='(2a6,4x,2z9.8,4x,8z3.2)', $ |
---|
24 | ; !version.arch, !version.os, long(!dpi,0,2), byte(!dpi,0,8) |
---|
25 | ; x86 linux 54442d18 400921fb 18 2d 44 54 fb 21 09 40 |
---|
26 | ; sparc sunos 400921fb 54442d18 40 09 21 fb 54 44 2d 18 |
---|
27 | ; alpha vms 0fda4149 68c0a221 49 41 da 0f 21 a2 c0 68 |
---|
28 | ; (Beginning with IDL 5.1, Alpha VMS uses IEEE representation as well.) |
---|
29 | ; |
---|
30 | ; @history |
---|
31 | ; 19 Dec 1997 Originally a news posting by David Fanning. |
---|
32 | ; (Re: bits from bytes) |
---|
33 | ; 20 Dec 1997 "Complete" rewrite: eliminate loops. |
---|
34 | ; 22 Dec 1997 Bit shift instead of exponentiation, return byte |
---|
35 | ; array, handle input arrays. |
---|
36 | ; Think about double and complex types. |
---|
37 | ; 22 Sep 1998 Complete rewrite: reduce every numerical type to |
---|
38 | ; single bytes. Check that big and little endian machines |
---|
39 | ; return exactly the same results (if IEEE). |
---|
40 | ; 7 May 2003 Added newish data types, unsigned and long64. BT |
---|
41 | ; |
---|
42 | ; @version |
---|
43 | ; $Id$ |
---|
44 | ; |
---|
45 | ;- |
---|
46 | ; |
---|
47 | FUNCTION binary, number |
---|
48 | ; |
---|
49 | compile_opt idl2, strictarrsubs |
---|
50 | ; |
---|
51 | s = size(number) |
---|
52 | type = s[s[0] + 1] |
---|
53 | n_no = s[s[0] + 2] |
---|
54 | ; Numerical types: (will have to be completed if IDL adds double-long, ...) |
---|
55 | ; 1: byte (1-byte unsigned integer) |
---|
56 | ; 2: integer (2-byte signed integer) |
---|
57 | ; 3: long (4-byte signed integer) |
---|
58 | ; 4: floating-point (4-byte, single precision) |
---|
59 | ; 5: double-precision (8-byte, double precision) |
---|
60 | ; 6: complex (2x4-byte, single precision) |
---|
61 | ; 9: double-complex (2x8-byte, double precision) |
---|
62 | ; 12: uInt (2-byte, unsigned integer) |
---|
63 | ; 13: uLong (4-byte, unsigned integer) |
---|
64 | ; 14: Long64 (8-byte, signed integer) |
---|
65 | ; 15: uLong64 (8-byte, unsigned integer) |
---|
66 | ; Non-numerical types: |
---|
67 | ; 0: undefined, 7: string, 8: structure, 10: pointer, 11: object reference |
---|
68 | ; nbyt = [0, 1, 2, 4, 4, 8, 8, 0, 0, 16, 0, 0] ; number of bytes per type |
---|
69 | ; code = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15] |
---|
70 | nbyt = [0, 1, 2, 4, 4, 8, 8, 0, 0, 16, 0, 0, 2, 4, 8, 8] |
---|
71 | ntyp = nbyt[type] |
---|
72 | if ntyp eq 0 then message, 'Invalid argument (must be numerical type).' |
---|
73 | bits = [128, 64, 32, 16, 8, 4, 2, 1] ; = ishft(1b, 7-indgen(8)) |
---|
74 | ; For correct array handling and byte comparison, 'number' and 'bits' require |
---|
75 | ; same dimensions -> numvalue and bitvalue |
---|
76 | bitvalue = ((bits)[*, intarr(ntyp)])[*, *, intarr(n_no)] |
---|
77 | little_endian = (byte(1, 0, 1))[0] |
---|
78 | ; In case of complex type and little endian machine, swap the two float values |
---|
79 | ; before the complete second dimension is reversed at returning. |
---|
80 | if (type eq 6 or type eq 9) and little_endian then $ ; type complex |
---|
81 | numvalue = reform((byte([number], 0, 1, ntyp/2, 2, n_no))$ |
---|
82 | [intarr(8), *, [1,0], *], 8, ntyp, n_no) $ |
---|
83 | else numvalue = (byte([number], 0, 1, ntyp, n_no))[intarr(8), *, *] |
---|
84 | ; On little endian machines, the second dimension of the return value must |
---|
85 | ; be reversed. |
---|
86 | if little_endian AND type NE 1 then $ |
---|
87 | return, reverse((numvalue and bitvalue) ne 0, 2) else $ |
---|
88 | return, (numvalue and bitvalue) ne 0 |
---|
89 | end |
---|