字节、十六进制、字符串互转

说明:此文章仅做笔记记录,内容非原创,侵权删

经测试,串口接收中文内容使用Encoding.Default格式可以避免中文乱码


 

public class ByteMethod
 {
    
     /// <summary>
     /// 十六进制字符串转字符串
     /// </summary>
     /// <param name="hs">十六进制字符串</param>
     /// <param name="encode">字符编码格式</param>
     /// <returns></returns>
     public static string HexStringToString(string hs, Encoding encode)
     {
         //并去掉空字符
         string[] chars = hs.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
         byte[] b = new byte[chars.Length];
         //逐个字符变为16进制字节数据
         for (int i = 0; i < chars.Length; i++)
         {
             b[i] = Convert.ToByte(chars[i], 16);
         }
         //按照指定编码将字节数组变为字符串
         return encode.GetString(b);
     }
     /// <summary>
     /// 字符转换(十六进制字符串转字节数组)
     /// </summary>
     /// <param name="txt"></param>
     /// <returns></returns>
     public static Byte[] SendKeyByte(string txt)
     {
         string[] key = txt.Split(' ');//空格
         Byte[] BSendTemp = new Byte[key.Length];
         for (int i = 0; i < BSendTemp.Length; i++)
         {
             BSendTemp[i] = Convert.ToByte(key[i], 16);
         }
         return BSendTemp;
     }
     /// <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;
     }
     /// <summary>
     /// 十进制字符转16进制
     /// </summary>
     /// <param name="ss"></param>
     /// <returns></returns>
     public static string strTohex(string ss)
     {
         try
         {
             char[] values = ss.ToCharArray();
             string str = "";
             for (int i = 0; i < values.Length; i++)
             {
                 int hex = Convert.ToInt32(values[i]);
                 str += string.Format("{0:X}", hex) + " ";
             }
             return str;
         }
         catch (Exception)
         {


             return "";
         }

     }
     /// <summary>
     /// 科学记数转
     /// </summary>
     /// <param name="strData"></param>
     /// <returns></returns>
     public static decimal ChangeDataToD(string strData)
     {
         Decimal dData = 0.0M;
         if (strData.Contains("E"))
         {
             dData = Convert.ToDecimal(Decimal.Parse(strData.ToString(), System.Globalization.NumberStyles.Float));
         }
         return dData;

     }
     /// <summary>
     ///  ASCII码转字符:
     /// </summary>
     /// <param name="asciiCode"></param>
     /// <returns></returns>
     public static string ASCII_TO_Chr(int asciiCode)
     {
         if (asciiCode >= 0 && asciiCode <= 255)
         {
             System.Text.ASCIIEncoding asciiEncoding = new System.Text.ASCIIEncoding();
             byte[] byteArray = new byte[] { (byte)asciiCode };
             string strCharacter = asciiEncoding.GetString(byteArray);
             return (strCharacter);
         }
         else
         {
             throw new Exception("ASCII Code is not valid.");
         }
     }

     /// <summary>
     /// 图片路径转换为byte[]
     /// </summary>
     /// <param name="imagePath"></param>
     /// <returns></returns>
     public static byte[] GetPictureData(string imagePath)
     {
         FileStream fs = new FileStream(imagePath, FileMode.Open);
         byte[] byteData = new byte[fs.Length];
         fs.Read(byteData, 0, byteData.Length);
         fs.Close();
         return byteData;
     }
     /// <summary>
     /// byte[]格式转为图片
     /// </summary>
     /// <param name="bytes"></param>
     /// <returns></returns>
     public static Image BytesToImage(byte[] bytes)
     {
         MemoryStream ms = new MemoryStream(bytes);
         Image img = Image.FromStream(ms);
         return img;
     }
 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

执缨不祗

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值