1 Star 0 Fork 0

lhtzbj12/csharp-sm

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
HexUtils.cs 1.90 KB
一键复制 编辑 原始数据 按行查看 历史
lhtzbj12 提交于 2024-02-22 10:32 . 初始化
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CSharpSM
{
internal class HexUtils
{
private const string pattern = "^[a-f0-9]+$";
public static bool isHex(string value)
{
if (null == value)
{
return false;
}
return System.Text.RegularExpressions.Regex.IsMatch(value, pattern);
}
public static byte[] decode(String str)
{
if (null == str)
{
return null;
}
if (isHex(str))
{
return hexStrToByte(str);
}
return Org.BouncyCastle.Utilities.Encoders.Base64.Decode(str);
}
public static byte[] hexStrToByte(String hexStr)
{
if ((null == hexStr) || (hexStr.Length == 0))
{
return null;
}
char[] hexData = hexStr.ToCharArray();
int len = hexData.Length;
if ((len & 0x1) != 0)
{
throw new SystemException("Odd number of characters.");
}
byte[] out1 = new byte[len >> 1];
int i = 0;
for (int j = 0; j < len; i++)
{
int f = toDigit(hexData[j], j) << 4;
j++;
f |= toDigit(hexData[j], j);
j++;
out1[i] = ((byte)(f & 0xFF));
}
return out1;
}
private static int toDigit(char ch, int index)
{
int digit = Convert.ToInt32(ch.ToString(), 16);
//int digit = Character.digit(ch, 16);
if (digit == -1)
{
throw new SystemException("Illegal hexadecimal character " + ch + " at index " + index);
}
return digit;
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C#
1
https://gitee.com/lhtzbj12/csharp-sm.git
[email protected]:lhtzbj12/csharp-sm.git
lhtzbj12
csharp-sm
csharp-sm
master

搜索帮助