ANSI文本格式读写
StreamReader sr = new StreamReader("yourFilePath", Encoding.Default); string strBody = sr.ReadToEnd(); sr.Close();
写文件 StreamWriter sw = new StreamWriter("yourFilePath", false, Encoding.Default); sw.Write("中文ABC"); sw.Close();
//字符Unicode
string s = "abCDlks";
//字符转Default(//8位无编码整数)
byte[] buffer = Encoding.Default.GetBytes(s);
//Default转Utf-8
string sU8 = Encoding.UTF8.GetString(buffer);
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) { ASCIIEncoding asciiEncoding = new ASCIIEncoding(); byte[] byteArray = new byte[] { (byte)asciiCode }; string strCharacter = asciiEncoding.GetString(byteArray); return (strCharacter); } else { throw new Exception("ASCII Code is not valid."); }
}
|
请发表评论