84 lines
2.1 KiB
C#
84 lines
2.1 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using Windows.Foundation;
|
|
using Windows.Storage.Streams;
|
|
|
|
namespace MonitoringTechnology.Common
|
|
{
|
|
public static class Extensions
|
|
{
|
|
public static async Task<T> GetResultAsync<T>(this IAsyncOperation<T> asyncOperation)
|
|
{
|
|
return await Task.Run(() =>
|
|
{
|
|
while (asyncOperation.Status == AsyncStatus.Started)
|
|
{
|
|
}
|
|
try
|
|
{
|
|
return asyncOperation.GetResults();//这里会异常
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
|
|
throw ex;
|
|
}
|
|
|
|
});
|
|
}
|
|
|
|
/// <summary>
|
|
/// 十进制转十六进制字符串
|
|
/// </summary>
|
|
/// <param name="data"></param>
|
|
/// <returns></returns>
|
|
public static string ToHex(this byte[] data)
|
|
{
|
|
return string.Join(" ", data.Select(b => $"{b:X2}"));
|
|
}
|
|
|
|
/// <summary>
|
|
/// 十进制转十六进制字符串
|
|
/// </summary>
|
|
/// <param name="data"></param>
|
|
/// <returns></returns>
|
|
public static string ToHexString(this byte[] data)
|
|
{
|
|
return string.Join(" ", data);
|
|
}
|
|
|
|
public static byte[] ToData(this string hex)
|
|
{
|
|
hex = hex.Replace(" ", "").Trim();
|
|
if (hex.Length % 2 == 1)
|
|
{
|
|
hex = $"0{hex}";
|
|
}
|
|
int len = hex.Length / 2;
|
|
byte[] data = new byte[len];
|
|
for (int i = 0; i < len; i++)
|
|
{
|
|
data[i] = Convert.ToByte(hex.Substring(i * 2, 2), 16);
|
|
}
|
|
return data;
|
|
}
|
|
|
|
public static byte[] ToBytes(this IBuffer data)
|
|
{
|
|
byte[] bytes = new byte[data.Length];
|
|
DataReader.FromBuffer(data).ReadBytes(bytes);
|
|
return bytes;
|
|
}
|
|
|
|
public static string ToHex(this IBuffer data)
|
|
{
|
|
return data.ToBytes().ToHex();
|
|
}
|
|
|
|
//
|
|
}
|
|
}
|