C# 如何在C语言中获取字符串的ASCII值#
我想用C#获取字符串中字符的ASCII值 如果字符串的值为“9Quality52Ty3”,则需要一个包含11个字符中每个字符的ASCII值的数组 如何获取C#中的ASCII值?这应该可以:C# 如何在C语言中获取字符串的ASCII值#,c#,encoding,ascii,C#,Encoding,Ascii,我想用C#获取字符串中字符的ASCII值 如果字符串的值为“9Quality52Ty3”,则需要一个包含11个字符中每个字符的ASCII值的数组 如何获取C#中的ASCII值?这应该可以: string s = "9quali52ty3"; byte[] ASCIIValues = Encoding.ASCII.GetBytes(s); foreach(byte b in ASCIIValues) { Console.WriteLine(b); } 你是说你只想要字母而不是数字?所以你
string s = "9quali52ty3";
byte[] ASCIIValues = Encoding.ASCII.GetBytes(s);
foreach(byte b in ASCIIValues) {
Console.WriteLine(b);
}
你是说你只想要字母而不是数字?所以你想要“质量”作为结果?您可以使用Char.isleter或Char.IsDigit逐个筛选它们
string s = "9quali52ty3";
StringBuilder result = new StringBuilder();
foreach(char c in s)
{
if (Char.IsLetter(c))
result.Add(c);
}
Console.WriteLine(result); // quality
string text=“ABCD”;
for(int i=0;i”+Char.ConvertToUtf32(text,i));
}
如果我没记错的话,ASCII值是数字的低七位的数字。如果您想要字符串中每个字符的字符码,可以执行以下操作:
char[] chars = "9quali52ty3".ToCharArray();
从
现在您有了一个字节的ASCII值数组。我得到以下信息:
57
113
117
97
108
105
53
50
116
121
51先前的回复者已经回答了这个问题,但没有提供标题让我期待的信息。我有一个返回一个字符串的方法,但是 我想要一个可以转换成十六进制的字符。下面的代码演示了我认为可以找到的内容,希望对其他人有所帮助
string s = "\ta£\x0394\x221A"; // tab; lower case a; pound sign; Greek delta;
// square root
Debug.Print(s);
char c = s[0];
int i = (int)c;
string x = i.ToString("X");
c = s[1];
i = (int)c;
x = i.ToString("X");
Debug.Print(c.ToString() + " " + i.ToString() + " " + x);
c = s[2];
i = (int)c;
x = i.ToString("X");
Debug.Print(c.ToString() + " " + i.ToString() + " " + x);
c = s[3];
i = (int)c;
x = i.ToString("X");
Debug.Print(c.ToString() + " " + i.ToString() + " " + x);
c = s[4];
i = (int)c;
x = i.ToString("X");
Debug.Print(c.ToString() + " " + i.ToString() + " " + x);
上述代码将以下内容输出到即时窗口:
a£Δ√
a 97 61
163英镑A3
Δ916 394
√ 8730 221A或在LINQ中:
string value = "9quali52ty3";
var ascii_values = value.Select(x => (int)x);
var as_hex = value.Select(x => ((int)x).ToString("X02"));
string value=“mahesh”;
//将字符串转换为字节[]。
字节[]asciiBytes=Encoding.ASCII.GetBytes(值);
for(int i=0;i
您可以使用以下方法删除:
此程序将接受多个字符并输出其ASCII值:
using System;
class ASCII
{
public static void Main(string [] args)
{
string s;
Console.WriteLine(" Enter your sentence: ");
s = Console.ReadLine();
foreach (char c in s)
{
Console.WriteLine((int)c);
}
}
}
我想用C#获取字符串中字符的ASCII值 在这个结构中,每个人都给出答案。 如果字符串的值为“9Quality52Ty3”,则需要一个包含11个字符中每个字符的ASCII值的数组 但在控制台中,我们工作坦率,所以我们得到一个字符并打印ASCII码,如果我错了,请更正我的答案
static void Main(string[] args)
{
Console.WriteLine(Console.Read());
Convert.ToInt16(Console.Read());
Console.ReadKey();
}
为什么不是老式的简单方法呢
public int[] ToASCII(string s)
{
char c;
int[] cByte = new int[s.Length]; / the ASCII string
for (int i = 0; i < s.Length; i++)
{
c = s[i]; // get a character from the string s
cByte[i] = Convert.ToInt16(c); // and convert it to ASCII
}
return cByte;
}
public int[]ToASCII(字符串s)
{
字符c;
int[]cByte=new int[s.Length];/ASCII字符串
对于(int i=0;i
//现在好多了;) 你是说你只想要字母而不是数字?所以你想要“质量”作为结果?因为谈论ASCII没有什么意义。我想要字符串中每个字符的ASCII,数字的ASCII以及单词“质量”的ASCII。你的意思是你想要字符串中每个字符的数字ASCII值,假设整个字符串可以用ASCII表示。您当前的措辞非常混乱。生成的代码是Unicode数字,可能包含非ASCII代码。这也是为给定字符串中的每个字符实现ASCII值的示例之一。此答案需要更多解释。如果你的同事英语更流利,我的解释非常简单,因为我的代码非常简单,没有任何变量,但使用了一个简单的概念如何打印ASCII值单个简单获取字符并将字符转换为整数值,因此请您执行此操作,如果我的解释有错,请您更正此操作,谢谢显示系统。字节[]。你需要遍历单词中的字符。不知道你是怎么做到的。帮助OP了解哪些是重要的。注意:
编码。ASCII
配置为为为ASCII字符集以外的字符发出替换字符(“?”)的ASCII代码单位。编码
类中提供了其他选项,包括引发异常。当然,还可以使用其他字符编码,尤其是UTF-8;人们应该质疑是否真正需要ASCII码。虽然这段代码可以解决这个问题,但如何以及为什么解决这个问题会真正有助于提高您的帖子质量,并可能导致更多的投票。请记住,你是在将来回答读者的问题,而不仅仅是现在提问的人。请在回答中添加解释,并说明适用的限制和假设。给出编译错误:“意外字符”。ToString之后的两个可能不应该在那里。
string value = "9quali52ty3";
var ascii_values = value.Select(x => (int)x);
var as_hex = value.Select(x => ((int)x).ToString("X02"));
string value = "mahesh";
// Convert the string into a byte[].
byte[] asciiBytes = Encoding.ASCII.GetBytes(value);
for (int i = 0; i < value.Length; i++)
{
Console.WriteLine(value.Substring(i, 1) + " as ASCII value of: " + asciiBytes[i]);
}
//Create a character to compare BOM
char byteOrderMark = (char)65279;
if (sourceString.ToCharArray()[0].Equals(byteOrderMark))
{
targetString = sourceString.Remove(0, 1);
}
using System;
class ASCII
{
public static void Main(string [] args)
{
string s;
Console.WriteLine(" Enter your sentence: ");
s = Console.ReadLine();
foreach (char c in s)
{
Console.WriteLine((int)c);
}
}
}
static void Main(string[] args)
{
Console.WriteLine(Console.Read());
Convert.ToInt16(Console.Read());
Console.ReadKey();
}
public int[] ToASCII(string s)
{
char c;
int[] cByte = new int[s.Length]; / the ASCII string
for (int i = 0; i < s.Length; i++)
{
c = s[i]; // get a character from the string s
cByte[i] = Convert.ToInt16(c); // and convert it to ASCII
}
return cByte;
}
string nomFile = "9quali52ty3";
byte[] nomBytes = Encoding.ASCII.GetBytes(nomFile);
string name = "";
foreach (byte he in nomBytes)
{
name += he.ToString("X02");
}
`
Console.WriteLine(name);