Debian Codegraph @

Sound/Osc/Coding/Cast.hs

raw symbols SCIP
1 -- | Bit-level type casts and byte layout string typecasts.
2 module Sound.Osc.Coding.Cast where
3
4 import Data.Char {- base -}
5 import Data.Word {- base -}
6
7 import Sound.Osc.Coding.Byte {- hosc3 -}
8 import Sound.Osc.Coding.Convert {- hosc -}
9
10 {- | The IEEE byte representation of a float.
11
12 >>> f32_w32 pi
13 1078530011
14
15 >>> f32_w32 (-7913907.5)
16 3404825447
17
18 >>> 23 ^ 7
19 3404825447
20 -}
21 f32_w32 :: Float -> Word32
22 f32_w32 = decode_word32 . encode_f32
23
24 {- | Inverse of 'f32_w32'.
25
26 >>> w32_f32 1078530011
27 3.1415927
28
29 >>> w32_f32 (23 ^ 7)
30 -7913907.5
31 -}
32 w32_f32 :: Word32 -> Float
33 w32_f32 = decode_f32 . encode_word32
34
35 {- | The IEEE byte representation of a double.
36
37
38 >>> f64_w64 pi
39 4614256656552045848
40
41 >>> f64_w64 1.6822072834e-314
42 3404825447
43 -}
44 f64_w64 :: Double -> Word64
45 f64_w64 = decode_word64 . encode_f64
46
47 {- | Inverse of 'f64_w64'.
48
49 >>> w64_f64 4614256656552045848
50 3.141592653589793
51
52 >>> w64_f64 (23 ^ 7)
53 1.6822072834e-314
54 -}
55 w64_f64 :: Word64 -> Double
56 w64_f64 = decode_f64 . encode_word64
57
58 -- | Transform a haskell string into a C string (a null suffixed byte string).
59 str_cstr :: String -> [Word8]
60 str_cstr s = map (int_to_word8 . ord) s ++ [0]
61
62 -- | Inverse of 'str_cstr'.
63 cstr_str :: [Word8] -> String
64 cstr_str = map (chr . word8_to_int) . takeWhile (/= 0)
65
66 -- | Transform a haskell string to a pascal string (a length prefixed byte string).
67 str_pstr :: String -> [Word8]
68 str_pstr s = int_to_word8 (length s) : map (int_to_word8 . ord) s
69
70 -- | Inverse of 'str_pstr'.
71 pstr_str :: [Word8] -> String
72 pstr_str = map (chr . word8_to_int) . drop 1