简体   繁体   中英

convert special char ‘ to hex value in C#

i know how to convert char to hex. but when it comes to special char normal conversion method is not working.

hex value for ' is 91 Ref : http://www.aboutmyip.com/AboutMyXApp/AsciiChart.jsp

my code

            string text =  "‘";
            char[] chars = text.ToCharArray();
            StringBuilder stringBuilder = new StringBuilder();
            foreach (char c in chars)
            {
                stringBuilder.Append(((Int16)c).ToString("x"));
            }
            String textAsHex = stringBuilder.ToString();

am getting output as 2018 but what should i get is 91

based on [@Richard,@harold,@jdweng & @500-Internal server error] feedback i found the solution to my problem.this will include extended ASCII table [convert character to hex value]

find the working code beolow

        private void button3_Click(object sender, EventArgs e)
        {
            string str = "‘";
            var encoding = System.Text.Encoding.Default;
            var values = encoding.GetBytes(str);
            Decimal dec = values[0];
            var hex = ToHexString(dec);
            string result = hex.ToString();
        }

       public static string ToHexString(Decimal dec)
        {
            var sb = new StringBuilder();
            while (dec > 1)
            {
                var r = dec % 16;
                dec /= 16;
                sb.Insert(0, ((int)r).ToString("X"));
            }
            return sb.ToString();
        }

Output is 91

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM