16进制字符串和byte数组的转化类

本文介绍了一个实用的字符串工具类,提供了将字节数组转换为十六进制字符串及从十六进制字符串还原为字节数组的功能。这些方法适用于多种应用场景,如数据校验、密码学等领域。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace Imps.Services.IDCService.Utility
{
    public static class StringUtil
    {
        public static string ConvertBytesToHex(byte[] arrByte)
        {
            StringBuilder sb = new StringBuilder();
            foreach (byte b in arrByte)
                sb.AppendFormat("{0:x2}", b);
            return sb.ToString();
        }


        public static byte[] ConvertHexToBytes(string value)
        {
            int len = value.Length / 2;
            byte[] ret = new byte[len];
            for (int i = 0; i < len; i++)
                ret[i] = Convert.ToByte(value.Substring(i * 2, 2), 16);
            return ret;
        }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值