惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

D
Docker
爱范儿
爱范儿
T
The Exploit Database - CXSecurity.com
量子位
T
Tailwind CSS Blog
T
Threatpost
The GitHub Blog
The GitHub Blog
AWS News Blog
AWS News Blog
云风的 BLOG
云风的 BLOG
K
Kaspersky official blog
P
Proofpoint News Feed
博客园 - 司徒正美
L
LangChain Blog
T
Threat Research - Cisco Blogs
C
CERT Recently Published Vulnerability Notes
罗磊的独立博客
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 叶小钗
S
Secure Thoughts
The Last Watchdog
The Last Watchdog
Spread Privacy
Spread Privacy
H
Hacker News: Front Page
T
Troy Hunt's Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Google DeepMind News
Google DeepMind News
W
WeLiveSecurity
A
Arctic Wolf
Apple Machine Learning Research
Apple Machine Learning Research
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
P
Proofpoint News Feed
T
Tor Project blog
T
The Blog of Author Tim Ferriss
I
Intezer
P
Privacy & Cybersecurity Law Blog
美团技术团队
N
Netflix TechBlog - Medium
博客园_首页
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
Vulnerabilities – Threatpost
Application and Cybersecurity Blog
Application and Cybersecurity Blog
G
Google Developers Blog
Attack and Defense Labs
Attack and Defense Labs
T
Tenable Blog
月光博客
月光博客
Stack Overflow Blog
Stack Overflow Blog
J
Java Code Geeks
腾讯CDC
Microsoft Security Blog
Microsoft Security Blog
A
About on SuperTechFans
Last Week in AI
Last Week in AI

博客园 - s3

RC4 加密算法asp版 汇编指令速查(转) 飘零网络验证-商业版3和金盾版1.5-研究中 virtualbox安装mac os x雪豹 最新的IP数据库备份 安装.net framework 4.0出错 “不信任的操作” ubuntu11.10安装redmine1.3 Fedora安装openresty Cosign安装-iis过滤模块介绍 fedora16安装mysql fedora16安装slickedit (转)SlickEdit的备份功能 (转)Slickedit的版本控制 (转)SlickEdit的按需显示 ubuntu11.10下使用slickedit16编译nginx(一) FreeBSD8.2安装GNOME(转) FreeBSD8.2安装(转) FreeBSD安装时的参考文章 Loong SSO安装部署
C#实现VB中的asc和chr函数,字符(含中文)转ASCII
s3 · 2013-01-30 · via 博客园 - s3
        /// <summary>
        /// 传入单个字符,得到字符的ASCII码
        /// </summary>
        /// <param name="chr">汤</param>
        /// <returns>-13120</returns>
        public static int ASCII(string chr)
        {
            Encoding ecode = Encoding.GetEncoding("GB18030");
            Byte[] codeBytes = ecode.GetBytes(chr);
            if (IsTwoBytesChar(chr))
            {
                // 双字节码为高位乘256,再加低位
                // 该为无符号码,再减65536
                return (int)codeBytes[0] * 256 + (int)codeBytes[1] - 65536;
            }
            else
            {
                return (int)codeBytes[0];
            }
        }
        /// <summary>
        /// 传入单个字符的ASCII码,得到ASCII码对应的字符(含双字节)
        /// </summary>
        /// <param name="asc">-13120</param>
        /// <returns>汤</returns>
        public static string Character(int asc)
        {
            //asc = asc + 65536;
            Encoding asciiEncoding = Encoding.GetEncoding("GB18030");
            Byte[] chrByte = BitConverter.GetBytes((short)asc);
            string strCharacter = string.Empty;
            if (asc < 0 || asc > 255)
            {
                Byte[] chrByteStr = new byte[2];
                chrByteStr[0] = chrByte[1];
                chrByteStr[1] = chrByte[0];
                strCharacter = asciiEncoding.GetString(chrByteStr);
            }
            else
            {
                Byte[] chrByteStr = new byte[1];
                chrByteStr[0] = chrByte[0];
                strCharacter = asciiEncoding.GetString(chrByteStr);
            }
            return (strCharacter);
        }

  下面对应单元测试

        [TestMethod]
        public void CharacterTest()
        {
            Assert.AreEqual(CommonFuntion.Character(-13120), "汤");
            Assert.AreEqual(CommonFuntion.Character(-10544), "中");
        }
        [TestMethod]
        public void ASCIITest()
        {
            Assert.AreEqual(CommonFuntion.ASCII("中"), -10544);
            Assert.AreEqual(CommonFuntion.ASCII("汤"), -13120);
        }
        [TestMethod]
        public void ASCIIAndCharTest()
        {
            Assert.AreEqual("中", CommonFuntion.Character(CommonFuntion.ASCII("中")));
            Assert.AreEqual("汤", CommonFuntion.Character(CommonFuntion.ASCII("汤")));
            Assert.AreEqual("A", CommonFuntion.Character(CommonFuntion.ASCII("A")));
            Assert.AreEqual("¥", CommonFuntion.Character(CommonFuntion.ASCII("¥")));
            Assert.AreEqual(-13120, CommonFuntion.ASCII(CommonFuntion.Character(-13120)));
            Assert.AreEqual(-10544, CommonFuntion.ASCII(CommonFuntion.Character(-10544)));
        }