using System; using System.Text; namespace DTS.Common.Utils { // Converts integral types to bytes but wraps the conversions so that // outgoing they are converted to network order and incoming they are // converted to host order. public class ByteConvertor { public static void Convert(byte[] input, int offset, out byte value) { // Nothing to do for single bytes value = input[offset]; } public static void Convert(byte[] input, int offset, out ushort value) { value = (ushort)(0 | input[offset + 0] << 8 | input[offset + 1] << 0); } public static void Convert(byte[] input, int offset, out short value) { value = (short)(0 | input[offset + 0] << 8 | input[offset + 1] << 0); } public static void Convert(byte[] input, int offset, out uint value) { value = 0 | (uint)input[offset + 0] << 24 | (uint)input[offset + 1] << 16 | (uint)input[offset + 2] << 8 | (uint)input[offset + 3] << 0; } public static void Convert(byte[] input, int offset, out int value) { value = input[offset + 0] << 24 | input[offset + 1] << 16 | input[offset + 2] << 8 | input[offset + 3] << 0; } public static void Convert(byte[] input, int offset, out ulong value) { value = 0 | (ulong)input[offset + 0] << 56 | (ulong)input[offset + 1] << 48 | (ulong)input[offset + 2] << 40 | (ulong)input[offset + 3] << 32 | (ulong)input[offset + 4] << 24 | (ulong)input[offset + 5] << 16 | (ulong)input[offset + 6] << 8 | (ulong)input[offset + 7] << 0; } public static void Convert(byte[] input, int offset, out long value) { value = 0 | (long)input[offset + 0] << 56 | (long)input[offset + 1] << 48 | (long)input[offset + 2] << 40 | (long)input[offset + 3] << 32 | (long)input[offset + 4] << 24 | (long)input[offset + 5] << 16 | (long)input[offset + 6] << 8 | (long)input[offset + 7] << 0; } public static void Convert(byte[] input, int offset, out float value) { value = BitConverter.ToSingle(input, offset); } public static void Convert(byte[] input, int offset, out double value) { value = BitConverter.ToDouble(input, offset); } public static void Convert(byte[] input, int offset, out string value) { var sb = new StringBuilder(); for (var i = offset; i < input.Length && input[i] != 0; i++) { sb.Append((char)input[i]); } value = sb.ToString(); } public static byte[] ToByteArray(string input) { var c = input.ToCharArray(); var rv = new byte[c.Length + 1]; for (var i = 0; i < c.Length; i++) rv[i] = (byte)c[i]; rv[c.Length] = 0; return rv; } public static byte[] ToByteArray(byte input) { var rv = new byte[1]; rv[0] = input; return rv; } public static byte[] ToByteArray(ushort input) { var rv = new byte[2]; rv[0] = (byte)((input >> 8) & 0xFF); rv[1] = (byte)((input >> 0) & 0xFF); return rv; } public static byte[] ToByteArray(short input) { var rv = new byte[2]; rv[0] = (byte)((input >> 8) & 0xFF); rv[1] = (byte)((input >> 0) & 0xFF); return rv; } public static byte[] ToByteArray(uint input) { var rv = new byte[4]; rv[0] = (byte)((input >> 24) & 0xFF); rv[1] = (byte)((input >> 16) & 0xFF); rv[2] = (byte)((input >> 8) & 0xFF); rv[3] = (byte)((input >> 0) & 0xFF); return rv; } public static byte[] ToByteArray(int input) { var rv = new byte[4]; rv[0] = (byte)((input >> 24) & 0xFF); rv[1] = (byte)((input >> 16) & 0xFF); rv[2] = (byte)((input >> 8) & 0xFF); rv[3] = (byte)((input >> 0) & 0xFF); return rv; } public static byte[] ToByteArray(ulong input) { var rv = new byte[8]; rv[0] = (byte)((input >> 56) & 0xFF); rv[1] = (byte)((input >> 48) & 0xFF); rv[2] = (byte)((input >> 40) & 0xFF); rv[3] = (byte)((input >> 32) & 0xFF); rv[4] = (byte)((input >> 24) & 0xFF); rv[5] = (byte)((input >> 16) & 0xFF); rv[6] = (byte)((input >> 8) & 0xFF); rv[7] = (byte)((input >> 0) & 0xFF); return rv; } public static byte[] ToByteArray(float input) { return BitConverter.GetBytes(input); } public static byte[] ToByteArray(double input) { return BitConverter.GetBytes(input); } } }