Files
cms-core-active/CMS_CORE_Library/Utils/Nc_Utils.cs
T
Lucio Maranta 0b2c6eed5f Fix repository
2017-11-16 09:08:11 +00:00

170 lines
4.9 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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<ushort> IntTOWordList(List<int> UintVal)
{
ushort low, high;
//initialize the new List
List<ushort> Word = new List<ushort>();
//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<int> WordTOIntList(List<ushort> UintVal)
{
int value;
//initialize the new List
List<int> Int = new List<int>();
//Fill the List
for (int i = 0; i < (UintVal.Count / 2); 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<ushort> UIntTOWordList(List<uint> UintVal)
{
ushort low, high;
//initialize the new List
List<ushort> Word = new List<ushort>();
//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<uint> WordTOUintList(List<ushort> UintVal)
{
uint value;
//initialize the new List
List<uint> Uint = new List<uint>();
//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
StreamWriter writer = new StreamWriter(fileReference);
writer.Write(fileContent);
// Close file Writer
writer.Close();
}
public static string ReadLocalFile(FileStream fileReference)
{
// Get File Content
StreamReader reader = new StreamReader(fileReference);
string fileContent = reader.ReadToEnd();
reader.Close();
return fileContent;
}
}
}