253 lines
7.7 KiB
C#
253 lines
7.7 KiB
C#
using System;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Runtime.InteropServices;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
|
|
namespace BMS_D1000H.Utils.Functions
|
|
{
|
|
public class ConvertTo
|
|
{
|
|
public static byte[] ShortToByteArray(short[] sdata, bool flag)
|
|
{
|
|
int j;
|
|
byte[] result;
|
|
result = new byte[sdata.Length * 2];
|
|
|
|
j = 0;
|
|
for (int i = 0; i < sdata.Length; i++)
|
|
{
|
|
if (flag == true)
|
|
{
|
|
result[j] = Convert.ToByte((sdata[i] >> 8) & 0xFF);
|
|
result[j + 1] = Convert.ToByte((sdata[i] >> 0) & 0xFF);
|
|
}
|
|
else
|
|
{
|
|
result[j + 1] = Convert.ToByte((sdata[i] >> 8) & 0xFF);
|
|
result[j] = Convert.ToByte((sdata[i] >> 0) & 0xFF);
|
|
}
|
|
j += 2;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
public static short TwoByteToShort(byte bdata1, byte bdata2, bool flag)
|
|
{
|
|
short result = 0;
|
|
byte[] bytes = { 0, 0 };
|
|
|
|
bytes[0] = bdata1;
|
|
bytes[1] = bdata2;
|
|
|
|
if (flag == true)
|
|
Array.Reverse(bytes);
|
|
|
|
try
|
|
{
|
|
result = BitConverter.ToInt16(bytes, 0);
|
|
}
|
|
catch //(Exception e)
|
|
{
|
|
//MessageBox.Show(e.ToString());
|
|
}
|
|
return result;
|
|
}
|
|
|
|
public static byte[] StringToByteArray(string hex)
|
|
{
|
|
return Enumerable.Range(0, hex.Length)
|
|
.Where(x => x % 2 == 0)
|
|
.Select(x => Convert.ToByte(hex.Substring(x, 2), 16))
|
|
.ToArray();
|
|
}
|
|
|
|
public static string ByteArrayToString(byte[] ba)
|
|
{
|
|
StringBuilder hex = new StringBuilder(ba.Length * 2);
|
|
for (int i = 0; i < ba.Length; i++)
|
|
hex.Append(ba[i].ToString("X2"));
|
|
return hex.ToString();
|
|
}
|
|
|
|
public static byte[] StructToByte(object obj)
|
|
{
|
|
int size = Marshal.SizeOf(obj);
|
|
byte[] arr = new byte[size];
|
|
IntPtr ptr = Marshal.AllocHGlobal(size);
|
|
Marshal.StructureToPtr(obj, ptr, true);
|
|
Marshal.Copy(ptr, arr, 0, size);
|
|
Marshal.FreeHGlobal(ptr);
|
|
return arr;
|
|
}
|
|
|
|
public static T ByteToStruct<T>(byte[] buffer) where T : struct
|
|
{
|
|
int size = Marshal.SizeOf(typeof(T));
|
|
if (size > buffer.Length)
|
|
throw new Exception();
|
|
IntPtr ptr = Marshal.AllocHGlobal(size);
|
|
Marshal.Copy(buffer, 0, ptr, size);
|
|
T obj = (T)Marshal.PtrToStructure(ptr, typeof(T));
|
|
Marshal.FreeHGlobal(ptr);
|
|
return obj;
|
|
}
|
|
|
|
//byte 배열을 구조체로
|
|
public static object ByteToStructure(byte[] data, Type type)
|
|
{
|
|
// 배열의 크기만큼 비관리 메모리 영역에 메모리를 할당한다.
|
|
IntPtr buff = Marshal.AllocHGlobal(data.Length);
|
|
|
|
// 배열에 저장된 데이터를 위에서 할당한 메모리 영역에 복사한다.
|
|
Marshal.Copy(data, 0, buff, data.Length);
|
|
|
|
// 복사된 데이터를 구조체 객체로 변환한다.
|
|
object obj = Marshal.PtrToStructure(buff, type);
|
|
|
|
// 비관리 메모리 영역에 할당했던 메모리를 해제함
|
|
Marshal.FreeHGlobal(buff);
|
|
|
|
// (((PACKET_DATA)obj).TotalBytes != data.Length)
|
|
// 구조체와 원래의 데이터의 크기 비교
|
|
//if (Marshal.SizeOf(obj) != data.Length)
|
|
//{
|
|
// // 크기가 다르면 null 리턴
|
|
// return null;
|
|
//}
|
|
// 구조체 리턴
|
|
return obj;
|
|
}
|
|
|
|
// 구조체를 byte 배열로
|
|
public static byte[] StructureToByte(object obj)
|
|
{
|
|
//((PACKET_DATA)obj).TotalBytes;
|
|
// 구조체에 할당된 메모리의 크기를 구한다.
|
|
int datasize = Marshal.SizeOf(obj);
|
|
|
|
|
|
// 비관리 메모리 영역에 구조체 크기만큼의 메모리를 할당한다.
|
|
IntPtr buff = Marshal.AllocHGlobal(datasize);
|
|
|
|
// 할당된 구조체 객체의 주소를 구한다.
|
|
Marshal.StructureToPtr(obj, buff, false);
|
|
|
|
// 구조체가 복사될 배열
|
|
byte[] data = new byte[datasize];
|
|
|
|
// 구조체 객체를 배열에 복사
|
|
Marshal.Copy(buff, data, 0, datasize);
|
|
|
|
// 비관리 메모리 영역에 할당했던 메모리를 해제함
|
|
Marshal.FreeHGlobal(buff);
|
|
|
|
// 배열을 리턴
|
|
return data;
|
|
}
|
|
|
|
public static string GetBits(string input)
|
|
{
|
|
StringBuilder sb = new StringBuilder();
|
|
foreach (byte b in Encoding.Unicode.GetBytes(input))
|
|
{
|
|
sb.Append(Convert.ToString(b, 2));
|
|
}
|
|
return sb.ToString();
|
|
}
|
|
|
|
public static string GetBits(byte[] bytes)
|
|
{
|
|
StringBuilder sb = new StringBuilder();
|
|
for (int i = 0; i < bytes.Length; i++)
|
|
{
|
|
sb.Append(Convert.ToString(bytes[i], 2));
|
|
}
|
|
return sb.ToString();
|
|
}
|
|
|
|
public static bool GetBit(byte b, int bitNumber)
|
|
{
|
|
BitArray ba = new BitArray(new byte[] { b });
|
|
return ba.Get(bitNumber);
|
|
}
|
|
|
|
public static bool GetBit(short b, int bitNumber)
|
|
{
|
|
short ba = Convert.ToInt16(1 << bitNumber);
|
|
if ((b & ba) != 0)
|
|
return true;
|
|
else
|
|
return false;
|
|
}
|
|
|
|
public static bool GetBit(byte[] b, int bitNumber)
|
|
{
|
|
BitArray ba = new BitArray(b);
|
|
return ba.Get(bitNumber);
|
|
}
|
|
|
|
public static byte[] GetBytes(string bitString)
|
|
{
|
|
byte[] result = Enumerable.Range(0, bitString.Length / 8).
|
|
Select(pos => Convert.ToByte(
|
|
bitString.Substring(pos * 8, 8),
|
|
2)
|
|
).ToArray();
|
|
|
|
List<byte> mahByteArray = new List<byte>();
|
|
for (int i = result.Length - 1; i >= 0; i--)
|
|
{
|
|
mahByteArray.Add(result[i]);
|
|
}
|
|
|
|
return mahByteArray.ToArray();
|
|
}
|
|
|
|
public static String ToBitString(BitArray bits)
|
|
{
|
|
var sb = new StringBuilder();
|
|
|
|
for (int i = bits.Count - 1; i >= 0; i--)
|
|
{
|
|
char c = bits[i] ? '1' : '0';
|
|
sb.Append(c);
|
|
}
|
|
|
|
return sb.ToString();
|
|
}
|
|
|
|
public static string StringToBinary(string data)
|
|
{
|
|
StringBuilder sb = new StringBuilder();
|
|
foreach (char c in data.ToCharArray())
|
|
{
|
|
sb.Append(Convert.ToString(c, 2).PadLeft(8, '0'));
|
|
}
|
|
return sb.ToString();
|
|
}
|
|
|
|
//public static string BinaryToString(string data)
|
|
//{
|
|
// List<Byte> byteList = new List<byte>();
|
|
// for (int i = 0; i < data.Length; i += 8)
|
|
// {
|
|
// byteList.Add(Convert.ToByte(data.Substring(i, 8), 2));
|
|
// }
|
|
// return Encoding.ASCII.GetString(byteList.ToArray());
|
|
//}
|
|
|
|
public static string BoolToStringYn(bool b)
|
|
{
|
|
return (b) ? "Y" : "N";
|
|
}
|
|
|
|
public static string BoolToBinaryText(bool b)
|
|
{
|
|
return (b) ? "1" : "0";
|
|
}
|
|
}
|
|
}
|