zoukankan      html  css  js  c++  java
  • C# 中10进制、16进制及其ASCII码之间转化

    C# 进制转化 主要用户串口通讯

     //字符串转byte 获取字节
                string s = "我喜欢7这个数字";
                byte[] sarr = System.Text.Encoding.Default.GetBytes(s);
                int len = sarr.Length;
    
                //10数字进制转换成ASCII码
                int num = 81;
                string si = ((char)num).ToString();//Q
    
                //10进制byte转换为ASCII码
                byte[] a1 = new byte[4];
                a1[0] = 66;
                a1[1] = 68;
                a1[2] = 72;
                a1[3] = 84;
                string ascii = Encoding.Default.GetString(a1);
    
                //16进制 16进制转ASCII
                int intAB = 0x42;
                string ass = ((char)intAB).ToString();
    
                //16进制转为10进制
                int intA = 0x42;
                string strA = "42";
                int A1 = Convert.ToInt32(intA);
                int A2 = Convert.ToInt32(strA,16);
    
                //10进制转为16进制
                int B1 = 66;
                string BB = Convert.ToString(B1,16);
                string BB2 = B1.ToString("x");
    
                //ASCII 字符转10进制byte
                string st = "38";
                byte[] ba = System.Text.ASCIIEncoding.Default.GetBytes(st);
    
                //10进制byte 转化为16进制byte
                byte[] newbyte = new byte[ba.Length];
                for (int i = 0; i < ba.Length; i++)
                {
                    newbyte[i] = Convert.ToByte(ba[i].ToString("x"));
                }
    
                //ASCII字符串转10进制数
                string tr = "385A";
                string d = "";
                for (int i = 0; i < tr.Length; i++)
                {
                    int ii = (int)Convert.ToChar(tr.Substring(i, 1));
                    d = d + " " + ii.ToString();//字符串
                }
                ba = System.Text.ASCIIEncoding.Default.GetBytes(tr);//10进制byte
    
    
                //ASCII字符串转16进制数
                string s2 = "385A";
                byte[] bad = System.Text.ASCIIEncoding.Default.GetBytes(s2);
                StringBuilder sb = new StringBuilder();
                foreach (byte b in bad)
                {
                    sb.Append(b.ToString("x") + " ");
                }
  • 相关阅读:
    windows 保留7天的文件
    同步
    bytes数据类型的转码问题:
    hashlib,logging,configparser模块
    面向对象 ---封装
    面向对象 -----多态
    面向对象 ---继承
    面向对象的命名空间与组合
    常用模块:
    匿名函数:
  • 原文地址:https://www.cnblogs.com/Duriyya/p/13640302.html
Copyright © 2011-2022 走看看