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

53 lines
1.3 KiB
C#

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;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Tool
{
public class HmiHelp
{
public static uint BitsToUint32(bool[] bits)
{
if (bits.Length > 32)
throw new ArgumentException("Bits array length must not exceed 32.");
uint result = 0;
for (int i = 0; i < bits.Length; i++)
{
// 如果当前位是 true则设置对应位置的位
if (bits[i])
{
result |= (uint)(1 << i); // 注意这里的位序是从最低位到最高位
}
}
return result;
}
public static uint ConvertBitsToUInt32(bool[] bits)
{
if (bits.Length != 32)
{
throw new ArgumentException("比特数组必须恰好包含 32 个比特");
}
uint result = 0;
// 遍历比特数组,将每个比特转换为相应的位,并设置到 uint32 中
for (int i = 0; i < 32; i++)
{
if (bits[i])
{
result |= (uint)(1 << (31 - i)); // 设置对应位置的比特为 1
}
}
return result;
}
}
}