file_utils/conversions/
i.rs1#[cfg(target_endian="big")]
2pub fn bytes_to_i16(x: &[u8; 2]) -> i16
3{
4 ((x[0] as i16) << 8) +
5 ((x[1] as i16) << 0)
6}
7
8#[cfg(target_endian="little")]
9pub fn bytes_to_i16(x: &[u8; 2]) -> i16
10{
11 ((x[1] as i16) << 8) +
12 ((x[0] as i16) << 0)
13}
14
15#[cfg(target_endian="big")]
16pub fn bytes_to_i32(x: &[u8; 4]) -> i32
17{
18 ((x[0] as i32) << 24) +
19 ((x[1] as i32) << 16) +
20 ((x[2] as i32) << 8) +
21 ((x[3] as i32) << 0)
22}
23
24#[cfg(target_endian="little")]
25pub fn bytes_to_i32(x: &[u8; 4]) -> i32
26{
27 ((x[3] as i32) << 24) +
28 ((x[2] as i32) << 16) +
29 ((x[1] as i32) << 8) +
30 ((x[0] as i32) << 0)
31}
32
33#[cfg(target_endian="big")]
34pub fn bytes_to_i64(x: &[u8; 8]) -> i64
35{
36 ((x[0] as i64) << 56) +
37 ((x[1] as i64) << 48) +
38 ((x[2] as i64) << 40) +
39 ((x[3] as i64) << 32) +
40 ((x[4] as i64) << 24) +
41 ((x[5] as i64) << 16) +
42 ((x[6] as i64) << 8) +
43 ((x[7] as i64) << 0)
44}
45
46#[cfg(target_endian="little")]
47pub fn bytes_to_i64(x: &[u8; 8]) -> i64
48{
49 ((x[7] as i64) << 56) +
50 ((x[6] as i64) << 48) +
51 ((x[5] as i64) << 40) +
52 ((x[4] as i64) << 32) +
53 ((x[3] as i64) << 24) +
54 ((x[2] as i64) << 16) +
55 ((x[1] as i64) << 8) +
56 ((x[0] as i64) << 0)
57}
58
59
60#[cfg(target_endian="big")]
61pub fn i16_to_bytes(x: i16) -> [u8; 2]
62{
63 [
64 ((x >> 8) & 0xff) as u8,
65 ((x >> 0) & 0xff) as u8
66 ]
67}
68
69#[cfg(target_endian="little")]
70pub fn i16_to_bytes(x: i16) -> [u8; 2]
71{
72 [
73 ((x >> 0) & 0xff) as u8,
74 ((x >> 8) & 0xff) as u8
75 ]
76}
77
78#[cfg(target_endian="big")]
79pub fn i32_to_bytes(x: i32) -> [u8; 4]
80{
81 [
82 ((x >> 24) & 0xff) as u8,
83 ((x >> 16) & 0xff) as u8,
84 ((x >> 8) & 0xff) as u8,
85 ((x >> 0) & 0xff) as u8
86 ]
87}
88
89#[cfg(target_endian="little")]
90pub fn i32_to_bytes(x: i32) -> [u8; 4]
91{
92 [
93 ((x >> 0) & 0xff) as u8,
94 ((x >> 8) & 0xff) as u8,
95 ((x >> 16) & 0xff) as u8,
96 ((x >> 24) & 0xff) as u8
97 ]
98}
99
100#[cfg(target_endian="big")]
101pub fn i64_to_bytes(x: i64) -> [u8; 8]
102{
103 [
104 ((x >> 56) & 0xff) as u8,
105 ((x >> 48) & 0xff) as u8,
106 ((x >> 40) & 0xff) as u8,
107 ((x >> 32) & 0xff) as u8,
108 ((x >> 24) & 0xff) as u8,
109 ((x >> 16) & 0xff) as u8,
110 ((x >> 8) & 0xff) as u8,
111 ((x >> 0) & 0xff) as u8
112 ]
113}
114
115#[cfg(target_endian="little")]
116pub fn i64_to_bytes(x: i64) -> [u8; 8]
117{
118 [
119 ((x >> 0) & 0xff) as u8,
120 ((x >> 8) & 0xff) as u8,
121 ((x >> 16) & 0xff) as u8,
122 ((x >> 24) & 0xff) as u8,
123 ((x >> 32) & 0xff) as u8,
124 ((x >> 40) & 0xff) as u8,
125 ((x >> 48) & 0xff) as u8,
126 ((x >> 56) & 0xff) as u8
127 ]
128}