* Filters/FilterPackage.cs, Filters/FilterRPM.cs,
[beagle.git] / Util / EndianConverter.cs
blobfeccd23240c5f454f209dcb106f5938ee9980f1a
1 //
2 // EndianConverter.cs
3 //
4 // Authors:
5 // Larry Ewing <lewing@novell.com>
6 //
7 //
8 // Copyright (C) 2004 - 2006 Novell, Inc (http://www.novell.com)
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 //
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 //
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 using System;
31 using System.Runtime.InteropServices;
33 namespace Beagle.Util {
34 public class EndianConverter {
35 public static uint Swap (uint val, bool little)
37 return (little != System.BitConverter.IsLittleEndian) ?
38 ((uint) ((((uint) (val) & (uint) 0x000000ffU) << 24) |
39 (((uint) (val) & (uint) 0x0000ff00U) << 8) |
40 (((uint) (val) & (uint) 0x00ff0000U) >> 8) |
41 (((uint) (val) & (uint) 0xff000000U) >> 24)))
42 : val;
45 public static ushort Swap (ushort val, bool little)
47 return (little != System.BitConverter.IsLittleEndian) ?
48 ((ushort) ((ushort)(val >> 8) | (ushort)(val << 8)))
49 : val;
52 public static ushort Swap (ushort val)
54 return ((ushort) ((ushort)(val >> 8) | (ushort)(val << 8)));
57 public static ulong Swap (ulong val, bool little)
59 return (little != System.BitConverter.IsLittleEndian) ?
60 ((ulong) ((((ulong) (val) & (ulong) 0x00000000000000ffU) << 56) |
61 (((ulong) (val) & (ulong) 0x000000000000ff00U) << 40) |
62 (((ulong) (val) & (ulong) 0x0000000000ff0000U) << 24) |
63 (((ulong) (val) & (ulong) 0x00000000ff000000U) << 8) |
64 (((ulong) (val) & (ulong) 0x000000ff00000000U) >> 8) |
65 (((ulong) (val) & (ulong) 0x0000ff0000000000U) >> 24) |
66 (((ulong) (val) & (ulong) 0x00ff000000000000U) >> 40) |
67 (((ulong) (val) & (ulong) 0xff00000000000000U) >> 56)))
68 : val;
71 public static byte [] GetBytes (uint val, bool little)
73 val = Swap (val, little);
74 return System.BitConverter.GetBytes (val);
77 public static byte [] GetBytes (ushort val, bool little)
79 val = Swap (val, little);
80 return System.BitConverter.GetBytes (val);
83 public static byte [] GetBytes (ulong val, bool little)
85 val = Swap (val, little);
86 return System.BitConverter.GetBytes (val);
89 public static ushort ToUInt16 (byte [] data, int position, bool little)
91 ushort val = System.BitConverter.ToUInt16 (data, position);
92 return Swap (val, little);
95 public static uint ToUInt32 (byte [] data, int position, bool little)
97 uint val = System.BitConverter.ToUInt32 (data, position);
98 return Swap (val, little);
101 public static float ToSingle (byte [] data, int position, bool little)
103 float retval;
104 unsafe {
105 uint * ptr;
106 ptr = (uint *)&retval;
107 *ptr = ToUInt32 (data, position, little);
109 return retval;
112 public static int ToInt32 (byte [] data, int position, bool little)
114 return unchecked ((int) ToUInt32 (data, position, little));
117 public static ulong ToUInt64 (byte [] data, int position, bool little)
119 ulong val = System.BitConverter.ToUInt64(data, position);
120 return Swap (val, little);