通用工具类库

This commit is contained in:
zqm
2025-12-12 10:46:27 +08:00
parent f98e21e7c5
commit a33ff6cf4f
6 changed files with 330 additions and 0 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.3 KiB

View File

@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.36617.4
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Utils", "Utils\Utils.csproj", "{0AAA20B8-F8B1-4C09-ADBF-E6FD650EE9D4}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{0AAA20B8-F8B1-4C09-ADBF-E6FD650EE9D4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0AAA20B8-F8B1-4C09-ADBF-E6FD650EE9D4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0AAA20B8-F8B1-4C09-ADBF-E6FD650EE9D4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0AAA20B8-F8B1-4C09-ADBF-E6FD650EE9D4}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {7019BD3D-89E7-4B0D-85B6-9E26BD8EF151}
EndGlobalSection
EndGlobal

View File

@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// 有关程序集的一般信息由以下
// 控制。更改这些特性值可修改
// 与程序集关联的信息。
[assembly: AssemblyTitle("Net40通用工具库")]
[assembly: AssemblyDescription("包含Base64、CRC编码等操作")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("JoyD")]
[assembly: AssemblyProduct("com.joyd.Utils")]
[assembly: AssemblyCopyright("Copyright © 2025 JoyD")]
[assembly: AssemblyTrademark("JoyD")]
[assembly: AssemblyCulture("zh-CN")]
// 将 ComVisible 设置为 false 会使此程序集中的类型
//对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型
//请将此类型的 ComVisible 特性设置为 true。
[assembly: ComVisible(false)]
// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID
[assembly: Guid("0aaa20b8-f8b1-4c09-adbf-e6fd650ee9d4")]
// 程序集的版本信息由下列四个值组成:
//
// 主版本
// 次版本
// 生成号
// 修订号
//
// 可以指定所有值,也可以使用以下所示的 "*" 预置版本号和修订号
//通过使用 "*",如下所示:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

View File

@@ -0,0 +1,215 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace JoyD.Windows.CS
{
/// <summary>
/// 通用工具和方法
/// </summary>
public static class Util
{
// CRC16/XMODEM算法实现带起始位置和长度
public static ushort CalculateCRC16(byte[] data, int startIndex, int length)
{
ushort crc = 0;
int endIndex = startIndex + length;
for (int i = startIndex; i < endIndex && i < data.Length; i++)
{
crc ^= (ushort)(data[i] << 8);
for (int j = 0; j < 8; j++)
{
if ((crc & 0x8000) != 0)
crc = (ushort)((crc << 1) ^ 0x1021);
else
crc <<= 1;
}
}
return crc;
}
// CRC32-MPEG2算法实现
public static uint CalculateCRC32(byte[] data)
{
return CalculateCRC32(0xFFFFFFFF, data);
}
// 支持增量计算的CRC32方法
public static uint CalculateCRC32(uint crc, byte[] data)
{
uint polynomial = 0x04C11DB7;
for (int i = 0; i < data.Length; i++)
{
crc ^= (uint)data[i] << 24;
for (int j = 0; j < 8; j++)
{
if ((crc & 0x80000000) != 0)
crc = (crc << 1) ^ polynomial;
else
crc <<= 1;
}
}
return crc;
}
// 计算文件的CRC32通过文件路径
public static uint CalculateFileCRC32(string filePath)
{
using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
return CalculateCRC32(fs);
}
}
// 计算文件的CRC32通过已打开的文件流
public static uint CalculateCRC32(FileStream fs)
{
uint crc = 0xFFFFFFFF;
uint polynomial = 0x04C11DB7;
// 保存当前位置
long originalPosition = fs.Position;
try
{
// 重置到文件开头
fs.Seek(0, SeekOrigin.Begin);
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = fs.Read(buffer, 0, buffer.Length)) > 0)
{
for (int i = 0; i < bytesRead; i++)
{
crc ^= (uint)buffer[i] << 24;
for (int j = 0; j < 8; j++)
{
if ((crc & 0x80000000) != 0)
crc = (crc << 1) ^ polynomial;
else
crc <<= 1;
}
}
}
}
finally
{
// 恢复到原始位置
fs.Seek(originalPosition, SeekOrigin.Begin);
}
return crc;
}
// byte[]转base64编码
public static string ByteArrayToBase64(byte[] data)
{
return Convert.ToBase64String(data);
}
// base64转byte[]编码
public static byte[] Base64ToByteArray(string base64)
{
return Convert.FromBase64String(base64);
}
// 构建不含前缀的FTPC命令返回byte[],直接处理字节流)
public static byte[] BuildRawFTPCommand(char command, params object[] parameters)
{
using (MemoryStream ms = new MemoryStream())
{
// 写入命令ASCII编码的单个字符
ms.WriteByte((byte)command);
// 添加参数(参数之间没有间隔,直接写入字节)
foreach (var param in parameters)
{
if (param is long)
{
// filesize24或addr24: 3字节大端序
long value = (long)param;
// 使用位运算直接获取24位值的三个字节大端序
byte byte1 = (byte)((value >> 16) & 0xFF); // 最高位字节
byte byte2 = (byte)((value >> 8) & 0xFF); // 中间字节
byte byte3 = (byte)(value & 0xFF); // 最低位字节
// 直接写入三个字节
ms.WriteByte(byte1);
ms.WriteByte(byte2);
ms.WriteByte(byte3);
}
else if (param is ushort)
{
// N16: 16位大端序数据长度
ushort value = (ushort)param;
// 使用位运算直接获取16位值的两个字节大端序
byte byte1 = (byte)((value >> 8) & 0xFF); // 高8位字节
byte byte2 = (byte)(value & 0xFF); // 低8位字节
// 直接写入两个字节
ms.WriteByte(byte1);
ms.WriteByte(byte2);
}
else if (param is uint)
{
// 处理uint类型如CRC32值
uint value = (uint)param;
// 使用位运算直接获取32位值的四个字节大端序
byte byte1 = (byte)((value >> 24) & 0xFF); // 最高位字节
byte byte2 = (byte)((value >> 16) & 0xFF); // 次高位字节
byte byte3 = (byte)((value >> 8) & 0xFF); // 次低位字节
byte byte4 = (byte)(value & 0xFF); // 最低位字节
// 直接写入四个字节
ms.WriteByte(byte1);
ms.WriteByte(byte2);
ms.WriteByte(byte3);
ms.WriteByte(byte4);
}
else if (param is string)
{
// 字符串参数直接写入ASCII字节
string strValue = (string)param;
byte[] strBytes = Encoding.ASCII.GetBytes(strValue);
ms.Write(strBytes, 0, strBytes.Length);
}
else if (param is byte[])
{
// byte[]参数直接写入
byte[] data = (byte[])param;
ms.Write(data, 0, data.Length);
}
}
// 获取当前流中的所有字节用于计算CRC16
byte[] currentBytes = ms.ToArray();
// 计算CRC16
ushort crc16 = CalculateCRC16(currentBytes, 0, currentBytes.Length);
// 写入CRC16大端序
byte crcByte1 = (byte)((crc16 >> 8) & 0xFF); // 高8位
byte crcByte2 = (byte)(crc16 & 0xFF); // 低8位
ms.WriteByte(crcByte1);
ms.WriteByte(crcByte2);
// 返回完整的字节数组
return ms.ToArray();
}
}
// 构建完整通信命令接受byte[]输入)
public static string BuildCommunicationCommand(byte[] rawFTPCommand)
{
string base64 = Convert.ToBase64String(rawFTPCommand);
return $"set(LFMGR: FTPC, \"{base64}\")";
}
}
}

View File

@@ -0,0 +1,54 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{0AAA20B8-F8B1-4C09-ADBF-E6FD650EE9D4}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>JoyD.Windows.CS</RootNamespace>
<AssemblyName>Utils</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<Deterministic>true</Deterministic>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<DocumentationFile>bin\Debug\Utils.xml</DocumentationFile>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup>
<ApplicationIcon>Utils.ico</ApplicationIcon>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Util.cs" />
</ItemGroup>
<ItemGroup>
<Content Include="Utils.ico" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.3 KiB