You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

307 lines
9.0 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using System.Text;
using System;
namespace VOL.Core.Utils
{
public class DataConvertUtil
{
/// <summary>
/// 赋值string string => ushort[]
/// </summary>
/// <param name="src"></param>
/// <param name="start"></param>
/// <param name="value"></param>
/// <returns></returns>
public static void SetString(ushort[] src, int start, string value)
{
byte[] bytesTemp = Encoding.UTF8.GetBytes(value);
ushort[] dest = Bytes2Ushorts(bytesTemp);
dest.CopyTo(src, start);
}
/// <summary>
/// 获取string ushort[] => string
/// </summary>
/// <param name="src"></param>
/// <param name="start"></param>
/// <param name="len"></param>
/// <returns>string</returns>
public static string GetString(ushort[] src, int start, int len)
{
ushort[] temp = new ushort[len];
for (int i = 0; i < len; i++)
{
temp[i] = src[i + start];
}
byte[] bytesTemp = Ushorts2Bytes(temp);
//string res = Encoding.UTF8.GetString(bytesTemp).Trim(new char[] { '\0' }); // 去除字符串左右端的指定内容
string res = Encoding.UTF8.GetString(bytesTemp).Trim();
return res;
}
/// <summary>
/// 赋值Real float => ushort[]
/// </summary>
/// <param name="src"></param>
/// <param name="start"></param>
/// <param name="value"></param>
public static void SetReal(ushort[] src, int start, float value)
{
byte[] bytes = BitConverter.GetBytes(value);
ushort[] dest = Bytes2Ushorts(bytes);
dest.CopyTo(src, start);
}
/// <summary>
/// 获取float ushort[] => float
/// </summary>
/// <param name="src"></param>
/// <param name="start"></param>
/// <returns>float</returns>
public static float GetReal(ushort[] src, int start)
{
try
{
ushort[] temp = new ushort[2];
for (int i = 0; i < 2; i++)
{
temp[i] = src[i + start];
}
byte[] bytesTemp = Ushorts2Bytes(temp,false);
// BitConverter默认是小端转换如果是大端顺序数组接收需要先反序字节顺序
Array.Reverse(bytesTemp);
float res = BitConverter.ToSingle(bytesTemp, 0);
return res;
} catch (Exception e)
{
return 0;
}
}
/// <summary>
/// 赋值Short short => ushort[]
/// </summary>
/// <param name="src"></param>
/// <param name="start"></param>
/// <param name="value"></param>
public static void SetShort(ushort[] src, int start, short value)
{
byte[] bytes = BitConverter.GetBytes(value);
ushort[] dest = Bytes2Ushorts(bytes);
dest.CopyTo(src, start);
}
/// <summary>
/// 获取short ushort[] => short
/// </summary>
/// <param name="src"></param>
/// <param name="start"></param>
/// <returns>short</returns>
public static short GetShort(ushort[] src, int start)
{
try
{
ushort[] temp = new ushort[1];
temp[0] = src[start];
byte[] bytesTemp = Ushorts2Bytes(temp);
short res = BitConverter.ToInt16(bytesTemp, 0);
return res;
}
catch (Exception e)
{
return 0;
}
}
/// <summary>
/// 赋值Short short => ushort[]
/// </summary>
/// <param name="src"></param>
/// <param name="start"></param>
/// <param name="value"></param>
public static void SetInt(ushort[] src, int start, int value)
{
byte[] bytes = BitConverter.GetBytes(value);
ushort[] dest = Bytes2Ushorts(bytes);
dest.CopyTo(src, start);
}
/// <summary>
/// 获取short ushort[] => int
/// </summary>
/// <param name="src"></param>
/// <param name="start"></param>
/// <returns>short</returns>
public static int GetInt(ushort[] src, int start)
{
try {
ushort[] temp = new ushort[2];
temp[0] = src[start];
temp[1] = src[start + 1]; // 0 100
Array.Reverse(temp);
byte[] bytesTemp = Ushorts2Bytes(temp);
int res = BitConverter.ToInt32(bytesTemp, 0);
return res;
} catch (Exception e)
{
return 0;
}
}
/// <summary>
/// 获取short ushort[] => long
/// </summary>
/// <param name="src"></param>
/// <param name="start"></param>
/// <returns>short</returns>
public static long GetLong(ushort[] src, int start)
{
try {
ushort[] temp = new ushort[4];
temp[0] = src[start];
temp[1] = src[start + 1];
temp[2] = src[start + 2];
temp[3] = src[start + 3];
Array.Reverse(temp);
byte[] bytesTemp = Ushorts2Bytes(temp);
long res = BitConverter.ToInt64(bytesTemp, 0);
return res;
} catch (Exception e)
{
return 0;
}
}
/// <summary>
/// 获取bool数组 ushort[] => bool[]
/// </summary>
/// <param name="src"></param>
/// <param name="start"></param>
/// <returns>short</returns>
public static bool[] GetBools(ushort[] src, int start, int num)
{
ushort[] temp = new ushort[num];
for (int i = start; i < start + num; i++)
{
temp[i] = src[i + start];
}
Array.Reverse(temp); // 反序,小端读取
byte[] bytes = Ushorts2Bytes(temp);
bool[] res = Bytes2Bools(bytes);
return res;
}
// byte[] => bool[]
private static bool[] Bytes2Bools(byte[] b)
{
bool[] array = new bool[8 * b.Length];
for (int i = 0; i < b.Length; i++)
{
for (int j = 0; j < 8; j++)
{
array[i * 8 + j] = (b[i] & 1) == 1;//判定byte的最后一位是否为1若为1则是true否则是false
b[i] = (byte)(b[i] >> 1);//将byte右移一位
}
}
return array;
}
// bool[] => byte
private static byte Bools2Byte(bool[] array)
{
if (array != null && array.Length > 0)
{
byte b = 0;
for (int i = 0; i < 8; i++)
{
if (array[i])
{
byte nn = (byte)(1 << i);//左移一位相当于×2
b += nn;
}
}
return b;
}
return 0;
}
// byte[] => ushort[]
private static ushort[] Bytes2Ushorts(byte[] src, bool reverse = false)
{
int len = src.Length;
byte[] srcPlus = new byte[len + 1];
src.CopyTo(srcPlus, 0);
int count = len >> 1;
if (len % 2 != 0)
{
count += 1;
}
ushort[] dest = new ushort[count];
if (reverse)
{
for (int i = 0; i < count; i++)
{
dest[i] = (ushort)(srcPlus[i * 2] << 8 | srcPlus[2 * i + 1] & 0xff);
}
}
else
{
for (int i = 0; i < count; i++)
{
dest[i] = (ushort)(srcPlus[i * 2] & 0xff | srcPlus[2 * i + 1] << 8);
}
}
return dest;
}
// ushort[] => byte[]
private static byte[] Ushorts2Bytes(ushort[] src, bool reverse = false)
{
int count = src.Length;
byte[] dest = new byte[count << 1];
if (reverse) // 大端
{
for (int i = 0; i < count; i++)
{
dest[i * 2] = (byte)(src[i] >> 8);
dest[i * 2 + 1] = (byte)(src[i] >> 0);
}
}
else // 小端
{
for (int i = 0; i < count; i++)
{
dest[i * 2] = (byte)(src[i] >> 0);
dest[i * 2 + 1] = (byte)(src[i] >> 8);
}
}
return dest;
}
}
}