c#的简单用法

1 将字符串转成int型 普通的 仅限数字型的

 int temp = int.Parse(hex.Substring(index, 2));   //------------将字符串转为整数 01为01

2 将字符串转成int型的 字符串是16进制的数字 如0a

 int temp = int.Parse(hex.Substring(index, 2),System.Globalization.NumberStyles.HexNumber);

如 0a 为 10

3 将 16 进制形式的字符串转成 10进制数组

private static byte[] strToHexByte(string hexString)  //----------------将16进制的字符串转成10进制的字节数组
{
    byte[] returnBytes = null;

    if(!string.IsNullOrEmpty(hexString))  //----------代表有效
    {
        hexString.Replace(" ", "");  //---------去掉空格

        if(hexString.Length % 2 != 0)
        {
           hexString += " ";  //---------变成偶数
        }

         returnBytes = new byte[hexString.Length / 2];

         int index = 0;

         for(int i = 0; i < returnBytes.Length; i ++)
         {
               returnBytes[i] = Convert.ToByte(hexString.Substring(index, 2), 16);  //-----------每一次取两位数 按照16进制转成10进制字节数组
               index += 2;
         }
    }

    return returnBytes;
}

4 string.Empty 是对直接赋值 “ “ 的优化写法

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值