Files
cms-core-active/CMS_CORE_Library/Utils/Nc_Utils.cs
T
Nicola Carminati f535c6e5b4
2017-10-05 06:25:17 +00:00

147 lines
4.3 KiB
C#

using System;
using System.Collections.Generic;
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;
}
}
}