C# ASCII和字符串相互转换

一万个项目-031

项目需求

如何将ASCII和字符串相互转换

知识点

string

csharp 复制代码
Stirng.Empty
  • 表示空字符串。 此字段为只读。此字段的值为零长度字符串""。
  • string为引用数据类型。会在内存的栈和堆上分配存储空间。因此string.Empty与""都会在栈上保存一个地址,这个地址占4字节,指向内存堆中的某个长度为0的空间,这个空间保存的是string.Empty的实际值。
  • 区别于null,null 只在栈上分配了空间,在堆上没有分配

out

可以在两个上下文中使用 out 关键字:

  • 作为 参数修饰符,通过引用而不是值将参数传递给方法。

  • 在接口和委托的 泛型类型参数声明中 ,该声明指定类型参数是协变的。

    当方法需要返回多个值时,关键字 out 特别有用,因为可以使用多个 out 参数。

csharp 复制代码
public void Main()
    {
        double radiusValue = 3.92781;
        //Calculate the circumference and area of a circle, returning the results to Main().
        CalculateCircumferenceAndArea(radiusValue, out double circumferenceResult, out var areaResult);
        System.Console.WriteLine($"Circumference of a circle with a radius of {radiusValue} is {circumferenceResult}.");
        System.Console.WriteLine($"Area of a circle with a radius of {radiusValue} is {areaResult}.");
        Console.ReadLine();
    }

    //The calculation worker method.
    public static void CalculateCircumferenceAndArea(double radius, out double circumference, out double area)
    {
        circumference = 2 * Math.PI * radius;
        area = Math.PI * (radius * radius);
    }

Encoding

将字符串从一种编码转换为另一种编码,

编码(Encoding):将 Unicode 字符转换为字节序列的过程。

解码(Decoding):将字节序列转换回 Unicode 字符的过程。

方法

  • GetEncoding。返回指定代码页的编码。属于编码过程
csharp 复制代码
//返回与指定代码页名称关联的编码。
//Encoding.GetEncoding(String)
using System;
using System.Text;

public class SamplesEncoding  {

   public static void Main()  {

      // Get a UTF-32 encoding by codepage.
      Encoding e1 = Encoding.GetEncoding( 12000 );

      // Get a UTF-32 encoding by name.
      Encoding e2 = Encoding.GetEncoding( "utf-32" );

      // Check their equality.
      Console.WriteLine( "e1 equals e2? {0}", e1.Equals( e2 ) );
   }
}
/* 
This code produces the following output.

e1 equals e2? True

*/

编码表

常用编码格式:

  • GetBytes
    在派生类中重写时,将一组字符编码为一个字节序列。属于解码过程
csharp 复制代码
//GetBytes(Char[])
//在派生类中重写时,将指定字符数组中的所有字符编码为一个字节序列。

字符串转换为数组

字符串可以理解为在字符数组。所以对手winform控件textbox的text值,也可以作为字符数组,

csharp 复制代码
if(textBox1.Text!="")
{
    string chars_StrA = "hello";
    string chars_StrB=string.Empty;
    char[] txtChars01 = new char[10];
    char[] txtChars02 = new char[10];
    for (int i=0;i<textBox1.Text.Length;i++)
    {
        txtChars01[i] =textBox1.Text[i];
    }
    for(int j= 0; j< chars_StrA.Length; j++)
    {
        txtChars02[j] = chars_StrA[j];
        chars_StrB += txtChars02[j];
    }
}

代码

csharp 复制代码
  private void btn_ToASCII_Click(object sender, EventArgs e)
  {
      

      if(txt_char.Text!=string.Empty)
      {
          if(Encoding.GetEncoding("unicode").GetBytes(new char[] { txt_char.Text[0] })[1]==0)
          {
              txt_ASCII.Text = Encoding.GetEncoding("unicode").GetBytes(txt_char.Text)[0].ToString();
          }
          else
          {
              txt_ASCII.Text = string.Empty;
              MessageBox.Show("请输入字母!", "提示!");
          }
      }
  }

  private void btn_ToChar_Click(object sender, EventArgs e)
  {
      if(txt_ASCII2.Text!=string.Empty)
      {
          int P_int_Num;//定义整型局部变量
          if(int.TryParse(txt_ASCII2.Text,out P_int_Num))
          {
              txt_Char2.Text = ((char)P_int_Num).ToString();
          }
          else
          {
              MessageBox.Show("请输入正确ASCII码值", "错误");
          }
      }
      
  }

gitee链接

相关推荐
hhh3u3u3u16 小时前
Visual C++ 6.0中文版安装包下载教程及win11安装教程
java·c语言·开发语言·c++·python·c#·vc-1
加号316 小时前
【C#】实现沃德普线光控制器通信控制(附完整源码)
开发语言·c#
lzhdim17 小时前
SharpCompress:跨平台的 C# 压缩与解压库
开发语言·c#
~plus~19 小时前
.NET 8 C# 委托与事件实战教程
网络·c#·.net·.net 8·委托与事件·c#进阶
beyond谚语20 小时前
接口&抽象类
c#·接口隔离原则·抽象类
新手小新21 小时前
C#学习笔记1-在VS CODE部署C#开发环境
笔记·学习·c#
rockey6271 天前
AScript动态脚本多语言环境支持
sql·c#·.net·script·eval·function·动态脚本
ou.cs1 天前
c# SemaphoreSlim保姆级教程
开发语言·网络·c#
龙侠九重天1 天前
ML.NET 实战:快速构建分类模型
分类·数据挖掘·c#·.net
fengyehongWorld1 天前
C# 创建Worker,杀死指定程序的线程
c#