string str = "abc"; for(int i = 0; i < str.Length; i++) Console.WriteLine((int)str[i]);应该是这样
写个demo吧:foreach (char c in s.ToArray()) Console.Write("{0} ", (int)c);
System.Text.ASCIIEncoding asciiEncoding = new System.Text.ASCIIEncoding(); int intAsciiCode = (int)asciiEncoding.GetBytes(character)[0];
//字符串转换为ascii码 string s = "abc"; byte[] byteArray = new byte[s.Length]; byteArray = ASCIIEncoding.ASCII.GetBytes(s); string p = ""; for (int i = 0; i < byteArray.Length; i++) { p = p + byteArray[i]+","; } s = p; //ascii码转换为字符串 int aa = 44; byte[] ss = new byte[]{(byte)aa}; string sp=ASCIIEncoding.ASCII.GetString(ss); s = sp;
using System; using System.Collections.Generic; using System.Linq; using System.Text;namespace tmp1 { class Program { static void Main(string[] args) { string s = "hello world!"; foreach (char c in s.ToCharArray()) { Console.Write("{0} {1}\n",(int)c,c); } Console.WriteLine("------------------------\n"); int i = 0; while (i < s.Length) { Console.Write("{0} {1}\n",(int)s[i],s[i]); i++; } Console.ReadLine(); } } }
abc到底是什么? 如果只是字符串的话,string s = "abc"; 用索引取就行了 s[0],s[1],s[2]
string str = "abc";
for(int i = 0; i < str.Length; i++)
Console.WriteLine((int)str[i]);应该是这样
Console.Write("{0} ", (int)c);
int intAsciiCode = (int)asciiEncoding.GetBytes(character)[0];
string s = "abc";
byte[] byteArray = new byte[s.Length];
byteArray = ASCIIEncoding.ASCII.GetBytes(s);
string p = "";
for (int i = 0; i < byteArray.Length; i++)
{
p = p + byteArray[i]+",";
}
s = p;
//ascii码转换为字符串
int aa = 44;
byte[] ss = new byte[]{(byte)aa};
string sp=ASCIIEncoding.ASCII.GetString(ss);
s = sp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace tmp1
{
class Program
{
static void Main(string[] args)
{
string s = "hello world!";
foreach (char c in s.ToCharArray())
{
Console.Write("{0} {1}\n",(int)c,c);
} Console.WriteLine("------------------------\n");
int i = 0;
while (i < s.Length)
{
Console.Write("{0} {1}\n",(int)s[i],s[i]);
i++;
}
Console.ReadLine();
}
}
}
如果只是字符串的话,string s = "abc";
用索引取就行了
s[0],s[1],s[2]
道理是什么。
上面有更简单的代码:(int)s[i]
我想问,你用这么复杂的代码,必要性何在呢?请再指教
容易读懂,知道在干嘛。不会转化为Utf8之类的