zoukankan      html  css  js  c++  java
  • C#字符串和ASCII码的转换

    C# 字符转ASCII码,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());
  • 相关阅读:
    样式的使用
    样式的使用
    jqurey基础一
    jQuery三天复习.md
    webstorm快捷键大全
    计算机的进制与编码
    2016-4-29HTML标记的使用
    HTML的基本概况
    Apache Maven 入门篇 ( 上 )
    ehcache.xml 分布试缓存
  • 原文地址:https://www.cnblogs.com/JoshuaDreaming/p/1882068.html
Copyright © 2011-2022 走看看