zoukankan      html  css  js  c++  java
  • C# 16进制与字符串、字节数组之间的转换

    在串口通讯过程中,经常要用到 16进制与字符串、字节数组之间的转换

    字符串转16进制字节数组
     1         private static byte[] strToToHexByte(string hexString)
     2         {
     3             hexString = hexString.Replace(" """);
     4             if ((hexString.Length % 2) != 0)
     5                 hexString += " ";
     6             byte[] returnBytes = new byte[hexString.Length / 2];
     7             for (int i = 0; i < returnBytes.Length; i++)
     8                 returnBytes[i] = Convert.ToByte(hexString.Substring(i * 22), 16);
     9             return returnBytes;
    10         }
    字节数组转16进制字符串
     1         public static string byteToHexStr(byte[] bytes)
     2         {
     3             string returnStr = "";
     4             if (bytes != null)
     5             {
     6                 for (int i = 0; i < bytes.Length; i++)
     7                 {
     8                     returnStr += bytes[i].ToString("X2");
     9                 }
    10             }
    11             return returnStr;
    12         }
    从16进制转换成汉字
     1         public static string UnHex(string hex, string charset)
     2         {
     3             if (hex == null)
     4                 throw new ArgumentNullException("hex");
     5             hex = hex.Replace(",""");
     6             hex = hex.Replace("/n""");
     7             hex = hex.Replace("//""");
     8             hex = hex.Replace(" """);
     9             if (hex.Length % 2 != 0)
    10             {
    11                 hex += "20";//空格
    12             }
    13             // 需要将 hex 转换成 byte 数组。 
    14             byte[] bytes = new byte[hex.Length / 2];
    15 
    16             for (int i = 0; i < bytes.Length; i++)
    17             {
    18                 try
    19                 {
    20                     // 每两个字符是一个 byte。 
    21                     bytes[i] = byte.Parse(hex.Substring(i * 22),
    22                     System.Globalization.NumberStyles.HexNumber);
    23                 }
    24                 catch
    25                 {
    26                     // Rethrow an exception with custom message. 
    27                     throw new ArgumentException("hex is not a valid hex number!""hex");
    28                 }
    29             }
    30             System.Text.Encoding chs = System.Text.Encoding.GetEncoding(charset);
    31             return chs.GetString(bytes);
    32         }

    从汉字转换到16进制

     1 public static string ToHex(string s, string charset, bool fenge)
     2         {
     3             if ((s.Length % 2) != 0)
     4             {
     5                 s += " ";//空格
     6                 //throw new ArgumentException("s is not valid chinese string!");
     7             }
     8             System.Text.Encoding chs = System.Text.Encoding.GetEncoding(charset);
     9             byte[] bytes = chs.GetBytes(s);
    10             string str = "";
    11             for (int i = 0; i < bytes.Length; i++)
    12             {
    13                 str += string.Format("{0:X}", bytes[i]);
    14                 if (fenge && (i != bytes.Length - 1))
    15                 {
    16                     str += string.Format("{0}"",");
    17                 }
    18             }
    19             return str.ToLower();
    20         }
  • 相关阅读:
    爬虫1 爬虫介绍, requests模块, 代理(正向代理,反向代理), 爬梨视频, 自动登录网站, HTTP协议复习, 伪静态概念, 301和302状态码区别, http版本0.9 1.1 和2.0的区别
    数据结构 线性结构(数组[列表] ,链表 单链表的增删改查**, 线性结构的应用 队列 栈[函数的调用**]),非线性结构 树
    算法 时间复杂度, 空间复杂度, 冒泡排序**, 选择排序, 插入算法, 快速排序**, 希尔算法,计数排序, 二分法查找**
    量化分析 在线量化分析网站
    数据分析3 matplotlib绘图, 折线图(刻度与范围,标题,注释), 曲线图例, 过滤报警信息, 柱状图, 曲线图, 饼图
    [编织消息框架][netty源码分析]6 ChannelPipeline 实现类DefaultChannelPipeline职责与实现
    [编织消息框架][netty源码分析]5 EventLoopGroup 实现类NioEventLoopGroup职责与实现
    [编织消息框架][netty源码分析]4 EventLoop 实现类NioEventLoop职责与实现
    编程之路
    [编织消息框架][netty源码分析]3 EventLoop 实现类SingleThreadEventLoop职责与实现
  • 原文地址:https://www.cnblogs.com/eric_ibm/p/2322413.html
Copyright © 2011-2022 走看看