C#實(shí)現(xiàn)ASCII和字符串相互轉(zhuǎn)換的代碼示例
知識(shí)點(diǎn)
string
Stirng.Empty
- 表示空字符串。 此字段為只讀。此字段的值為零長(zhǎng)度字符串“”。
- string為引用數(shù)據(jù)類(lèi)型。會(huì)在內(nèi)存的棧和堆上分配存儲(chǔ)空間。因此string.Empty與“”都會(huì)在棧上保存一個(gè)地址,這個(gè)地址占4字節(jié),指向內(nèi)存堆中的某個(gè)長(zhǎng)度為0的空間,這個(gè)空間保存的是string.Empty的實(shí)際值。
- 區(qū)別于null,null 只在棧上分配了空間,在堆上沒(méi)有分配
out
可以在兩個(gè)上下文中使用 out 關(guān)鍵字:
- 作為 參數(shù)修飾符,通過(guò)引用而不是值將參數(shù)傳遞給方法。
- 在接口和委托的 泛型類(lèi)型參數(shù)聲明中 ,該聲明指定類(lèi)型參數(shù)是協(xié)變的。
- 當(dāng)方法需要返回多個(gè)值時(shí),關(guān)鍵字 out 特別有用,因?yàn)榭梢允褂枚鄠€(gè) out 參數(shù)。
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
將字符串從一種編碼轉(zhuǎn)換為另一種編碼,
編碼(Encoding):將 Unicode 字符轉(zhuǎn)換為字節(jié)序列的過(guò)程。
解碼(Decoding):將字節(jié)序列轉(zhuǎn)換回 Unicode 字符的過(guò)程。
方法
- GetEncoding。返回指定代碼頁(yè)的編碼。屬于編碼過(guò)程
//返回與指定代碼頁(yè)名稱關(guān)聯(lián)的編碼。
//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
在派生類(lèi)中重寫(xiě)時(shí),將一組字符編碼為一個(gè)字節(jié)序列。屬于解碼過(guò)程
//GetBytes(Char[]) //在派生類(lèi)中重寫(xiě)時(shí),將指定字符數(shù)組中的所有字符編碼為一個(gè)字節(jié)序列。
字符串轉(zhuǎn)換為數(shù)組
字符串可以理解為在字符數(shù)組。所以對(duì)手winform控件textbox的text值,也可以作為字符數(shù)組,
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];
}
}
代碼

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("請(qǐng)輸入字母!", "提示!");
}
}
}
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("請(qǐng)輸入正確ASCII碼值", "錯(cuò)誤");
}
}
}
到此這篇關(guān)于C#實(shí)現(xiàn)ASCII和字符串相互轉(zhuǎn)換的代碼示例的文章就介紹到這了,更多相關(guān)C# ASCII和字符串互轉(zhuǎn)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
在Winform框架界面中改變并存儲(chǔ)界面皮膚樣式的方法
下面小編就為大家分享一篇在Winform框架界面中改變并存儲(chǔ)界面皮膚樣式的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助2017-11-11
C#中通過(guò)API實(shí)現(xiàn)的打印類(lèi) 實(shí)例代碼
這篇文章介紹了,C#中通過(guò)API實(shí)現(xiàn)的打印類(lèi) 實(shí)例代碼,有需要的朋友可以參考一下2013-08-08
wpf實(shí)現(xiàn)超低延遲的RTMP或RTSP播放
這篇文章主要為大家詳細(xì)介紹了wpf如何實(shí)現(xiàn)超低延遲的RTMP或RTSP播放,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-04-04
c# 使用谷歌身份驗(yàn)證GoogleAuthenticator的示例
這篇文章主要介紹了c# 使用谷歌身份驗(yàn)證GoogleAuthenticator的示例,幫助大家更好的理解和使用c#,感興趣的朋友可以了解下2021-01-01
C#實(shí)現(xiàn)rabbitmq 延遲隊(duì)列功能實(shí)例代碼
本篇文章主要介紹了C#實(shí)現(xiàn)rabbitmq 延遲隊(duì)列功能實(shí)例代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-04-04

