如何在C#中获取字符的ASCII值

编程入门 行业动态 更新时间:2024-10-26 06:29:48
本文介绍了如何在C#中获取字符的ASCII值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

下面是我在C#中的字符串,我将其转换为字符数组& ;;需要获取字符串中每个字符的ASCII值.

Below is my string in C# which I am converting it to Character array & in need to get the ASCII value of each character in the string.

static void Main(string[] args) { string s = "Test"; var arr = s.ToCharArray(); foreach(var a in arr) { var n = Encoding.ASCII.GetByteCount(a.ToString()); Console.WriteLine(a); Console.WriteLine(n); } }

输出为

T 1 e 1 s 1 t 1

在谷歌搜索时,我得到了很多链接,但是没有一个可以满足我的需求.

On googling I got number of links but none of them suffice my need.

  • 如何在C#中获取字符串的ASCII值
  • www.codeproject/Questions/516802/ConvertingpluscharsplustoplusASCIIplusinplusC
  • How to get ASCII value of string in C#
  • www.codeproject/Questions/516802/ConvertingpluscharsplustoplusASCIIplusinplusC

我需要获取字符串中每个字符的ASCII值.???

I am in need to get the ASCII value of each character in string.???

高度赞赏任何帮助/建议.

Any help/suggestion highly appreciated.

推荐答案

string可以直接枚举为IEnumerable<char>.并且每个char都可以转换为整数以查看其UNICODE值"(代码点). UTF-16将ASCII的128个字符(0-127)映射到UNICODE代码点0-127(例如,请参见 en.wikipedia/wiki/Code_point ),因此您可以直接打印此号码.

A string can be directly enumerated to a IEnumerable<char>. And each char can be casted to a integer to see its UNICODE "value" (code point). UTF-16 maps the 128 characters of ASCII (0-127) to the UNICODE code points 0-127 (see for example en.wikipedia/wiki/Code_point), so you can directly print this number.

string s = "Test"; foreach (char a in s) { if (a > 127) { throw new Exception(string.Format(@"{0} (code \u{1:X04}) is not ASCII!", a, (int)a)); } Console.WriteLine("{0}: {1}", a, (int)a); }

更多推荐

如何在C#中获取字符的ASCII值

本文发布于:2023-11-16 16:18:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1605873.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符   如何在   ASCII

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!