using System; using System.Collections.Generic; using System.IO; namespace CMS_CORE.Utils { public static class Nc_Utils { //------------------------------------------------------------------------------------------------------------------------------------------------- // NC <-> .NET Conversions // INT -> 2 WORD public static void IntTOWord(int IntVal, out ushort LowWord, out ushort HighWord) { byte[] bytes = BitConverter.GetBytes(IntVal); //Split into 2 word LowWord = BitConverter.ToUInt16(bytes, 0); HighWord = BitConverter.ToUInt16(bytes, 2); } // 2 WORD -> INT public static void WordTOInt(out int IntVal, ushort LowWord, ushort HighWord) { byte[] bytes = new byte[4]; //fill the bytes from 2 word bytes[0] = BitConverter.GetBytes(LowWord)[0]; bytes[1] = BitConverter.GetBytes(LowWord)[1]; bytes[2] = BitConverter.GetBytes(HighWord)[0]; bytes[3] = BitConverter.GetBytes(HighWord)[1]; //Create the value IntVal = BitConverter.ToInt32(bytes, 0); } // INT -> 2 WORD (LIST) public static List IntTOWordList(List UintVal) { ushort low, high; //initialize the new List List Word = new List(); //Fill the List foreach (int val in UintVal) { IntTOWord(val, out low, out high); Word.Add(low); Word.Add(high); } return Word; } // 2 WORD -> INT (LIST) public static List WordTOIntList(List UintVal) { int value; //initialize the new List List Int = new List(); //Fill the List for (int i = 0; i < UintVal.Count; i = i + 2) { WordTOInt(out value, UintVal[i], UintVal[i + 1]); Int.Add(value); } return Int; } //------------------------------------------------------------------------------------------------------------------------------------------------- // NC <-> .NET Conversions // UINT -> 2 WORD public static void UIntTOWord(uint UintVal, out ushort LowWord, out ushort HighWord) { byte[] bytes = BitConverter.GetBytes(UintVal); //Split into 2 word LowWord = BitConverter.ToUInt16(bytes, 0); HighWord = BitConverter.ToUInt16(bytes, 2); } // 2 WORD -> UINT public static void WordTOUint(out uint UintVal, ushort LowWord, ushort HighWord) { byte[] bytes = new byte[4]; //fill the bytes from 2 word bytes[0] = BitConverter.GetBytes(LowWord)[0]; bytes[1] = BitConverter.GetBytes(LowWord)[1]; bytes[2] = BitConverter.GetBytes(HighWord)[0]; bytes[3] = BitConverter.GetBytes(HighWord)[1]; //Create the value UintVal = BitConverter.ToUInt32(bytes, 0); } // UINT -> 2 WORD (LIST) public static List UIntTOWordList(List UintVal) { ushort low, high; //initialize the new List List Word = new List(); //Fill the List foreach (uint val in UintVal) { UIntTOWord(val, out low, out high); Word.Add(low); Word.Add(high); } return Word; } // 2 WORD -> UINT (LIST) public static List WordTOUintList(List UintVal) { uint value; //initialize the new List List Uint = new List(); //Fill the List for (int i = 0; i < (UintVal.Count / 2); i = i + 2) { WordTOUint(out value, UintVal[i], UintVal[i + 1]); Uint.Add(value); } return Uint; } public static void WriteLocalFile(string fileContent, ref FileStream fileReference) { // Write Part Program using (StreamWriter writer = new StreamWriter(fileReference)) writer.Write(fileContent); } public static string ReadLocalFile(FileStream fileReference) { // Get File Content StreamReader reader = new StreamReader(fileReference); string fileContent = reader.ReadToEnd(); return fileContent; } public static string GetUntilOrEmpty(string text, string stopAt = "[") { // Get string unti if (!String.IsNullOrWhiteSpace(text)) { // Find character index int charLocation = text.IndexOf(stopAt, StringComparison.Ordinal); if (charLocation > 0) return text.Substring(0, charLocation); } return String.Empty; } public static IEnumerable DistinctBy (this IEnumerable source, Func keySelector) { HashSet seenKeys = new HashSet(); foreach (TSource element in source) { if (seenKeys.Add(keySelector(element))) { yield return element; } } } } }