zoukankan      html  css  js  c++  java
  • C# ASCII与字符串间相互转换

    引言:

    最近开始学习C#,在写串口助手小工具时遇到十六进制发送与字符发送之间转换的问题,

    小弟通过网络各路大神的帮助下,终于实现正确显示收发,小弟菜鸟一枚,不足之处还望各位批评指正O(∩_∩)O!

    其中主要是利用调用ASCIIEncoding类来实现(System.IO.ASCIIEncoding), 下面入正题:

    1、将字符转换为对应的ASCII:

    string str = textBox2.Text.Trim();   // 去掉字符串首尾处的空格
    char[] charBuf = str.ToArray();    // 将字符串转换为字符数组
    ASCIIEncoding charToASCII = new ASCIIEncoding();

    byte[] TxdBuf = new byte[charBuf.Length];    // 定义发送缓冲区;
    TxdBuf = charToASCII.GetBytes(charBuf);    // 转换为各字符对应的ASCII


    2、将ASCII转换为对应字符:

    byte[] RxdBuf = new byte[len];        //  定义接收缓冲区;

    for(int i = 0; i < len; i++)

    {

      ASCIIEncoding ASCIITochar = new ASCIIEncoding();
      char[] ascii = ASCIITochar.GetChars(RxdBuf);      // 将接收字节解码为ASCII字符数组
      textBox1.Text += ascii[i];

    }

  • 相关阅读:
    Perface(TCP/IP 协议族)
    CHAPTER 2 Database Environment
    Chapter 1 Introduction
    2. Instructions: Language of the computer (指令:计算机语言)
    sed命令
    磁盘配额
    外设,镜像
    磁盘及文件系统挂载
    网络客户端工具命令
    TCP协议
  • 原文地址:https://www.cnblogs.com/lihq-sharefield/p/6683684.html
Copyright © 2011-2022 走看看