欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#字符串和Acsii碼相互轉(zhuǎn)換

 更新時間:2023年02月06日 15:30:25   作者:新時代丘鳴山  
本文主要介紹了C#字符串和Acsii碼相互轉(zhuǎn)換,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

1,現(xiàn)在因?yàn)橛龅揭粋€讀取pdf文件文本信息遇到亂么問題,才找到這個文本字符串的編碼轉(zhuǎn)換的實(shí)現(xiàn)方式來判斷是否存在亂碼(0>亂碼>255):

C# 字符轉(zhuǎn)ASCII碼,ASCII碼轉(zhuǎn)字符

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碼轉(zhuǎn)字符:

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.");
   }
}

還有一個特殊的方式:直接獲取字符串的字節(jié)大小來區(qū)分

string str="abcd";
byte[] bytetest = System.Text.Encoding.Default.GetBytes(str.ToString());

到此這篇關(guān)于C#字符串和Acsii碼相互轉(zhuǎn)換的文章就介紹到這了,更多相關(guān)C#字符串和Acsii碼轉(zhuǎn)換內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論