字符轉ASCII碼,ASCII碼轉字符

public static int Asc(string character)
  
{
   
if (character.Length == 1)
   
{
    System.Text.ASCIIEncoding asciiEncoding 
= new System.Text.ASCIIEncoding();
    
int intAsciiCode = (int)asciiEncoding.GetBytes(character)[0];
    
return (intAsciiCode);
   }

   
else
   
{
    
throw new Exception("Character is not valid.");
   }


  }


ASCII碼轉字符:

public static string Chr(int asciiCode)
  
{
   
if (asciiCode >= 0 && asciiCode <= 255)
   
{
    System.Text.ASCIIEncoding asciiEncoding 
= new System.Text.ASCIIEncoding();
    
byte[] byteArray = new byte[] { (byte)asciiCode };
    
string strCharacter = asciiEncoding.GetString(byteArray);
    
return (strCharacter);
   }

   
else
   
{
    
throw new Exception("ASCII Code is not valid.");
   }

  }
 

 
發佈了5 篇原創文章 · 獲贊 0 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章