• C# BYTE[] 与16进制字符串互相转换


    https://www.cnblogs.com/mingjing/p/14473568.html

    byte[] 转16进制字符串

    方法一、

    byte[] resultArray = new byte[]{1,2,3,4,5,6,7,8,9};
    BitConverter.ToString(resultArray).Replace("-", "")

    方法二、

     
     
    /// <summary> 
    /// 字节数组转16进制字符串 
    /// </summary> 
    /// <param name="bytes"></param> 
    /// <returns></returns> 
    public static string byteToHexStr(byte[] bytes)
    {
    string returnStr = "";
    if (bytes != null)
    {
    for (int i = 0; i < bytes.Length; i++)
    {
    returnStr += bytes[i].ToString("X2");
    }
    }
    return returnStr;
    }
     
     

    16进制的字符串转为byte[]

     
     
    /// <summary>
    /// 将16进制的字符串转为byte[]
    /// </summary>
    /// <param name="hexString"></param>
    /// <returns></returns>
    public static byte[] StrToHexByte(string hexString)
    {
    hexString = hexString.Replace(" ", "");
    if ((hexString.Length % 2) != 0)
    hexString += " ";
    byte[] returnBytes = new byte[hexString.Length / 2];
    for (int i = 0; i < returnBytes.Length; i++)
    returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
    return returnBytes;
    }
     
     
  • 相关阅读:
    python-函数作用域
    python-yield
    python-内置函数
    python-迭代和递归
    Command /usr/bin/codesign failed with exit code 1
    vue-infinite-scroll 自动加载
    git登陆迁移 SourceTree 不能自动识别
    Xcode不自动提示代码
    NSTimer的循环引用
    iOS autolayout 代码,自定义间距
  • 原文地址:https://www.cnblogs.com/emanlee/p/15201182.html
Copyright © 2020-2023  润新知