1,现在因为遇到一个读取pdf文件文本信息遇到乱么问题,才找到这个文本字符串的编码转换的实现方式来判断是否存在乱码(0>乱码>255):

C# 字符转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.");
   }
}

还有一个特殊的方式:直接获取字符串的字节大小来区分

string str="abcd";
byte[] bytetest = System.Text.Encoding.Default.GetBytes(str.ToString());
原文地址:https://blog.csdn.net/zyzBulus/article/details/87913453

相关文章:

  • 2022-12-23
  • 2022-01-29
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-06
  • 2021-04-12
  • 2022-12-23
猜你喜欢
  • 2021-09-09
  • 2022-01-02
  • 2021-09-14
  • 2022-01-12
  • 2022-12-23
  • 2022-12-23
  • 2021-09-22
相关资源
相似解决方案