C# ASCII与字符的转换
using System;
using System.Text;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("输入要转为ascii码的字符");
string a = Console.ReadLine(); //接收字符串
try
{
Console.WriteLine(Asc(a)); //将字符串转为ascii码并打印
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
Console.WriteLine("输入要转为字符的ascii码");
string b = Console.ReadLine(); //接收ascii码
try
{
Console.WriteLine(Cha(b)); //将ascii码字符串转为并打印
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
/*字符转ascii码*/
private static int Asc(string s)
{
if (s.Length == 1)
{
ASCIIEncoding a = new ASCIIEncoding();
int b = (int)a.GetBytes(s)[0]; //利用ASCIIEncoding类的GetByte()方法转码
return b;
}
else
{
throw new Exception("String is not vaild");
}
}
/*ascii码转字符*/
private static char Cha(string s)
{
int a = int.Parse(s); //现将字符串转成int类型
if (a >= 0 && a <= 255) //若不在这个范围内,则不是字符
{
char c = (char)a; //利用类型强转得到字符
return c;
}
else
{
throw new Exception("String is not vaild");
}
}
}
}
C# Dictionary
using System;
using System.Collections.Generic;
namespace SampleList
{
class CustomDictionary
{
//定义一个字典变量
static Dictionary<int, Person> dicPerson = new Dictionary<int, Person>();
public static void LearnDictionaryInfo()
{
//添加键值
Person p1 = new Person("hjc", 22);
Person p2 = new Person("tf", 21);
dicPerson.Add(0, p1); //方式1
dicPerson[1] = p2; //方式2
//取值
Console.WriteLine("\n");
Console.WriteLine("取值 name:" + dicPerson[0].name + "—" + "age:" + dicPerson[0].age);
//改值
Console.WriteLine("\n");
dicPerson[1].age = 20;
Console.WriteLine("改值 name:" + dicPerson[1].name + "—" + "age:" + dicPerson[1].age);
//遍历key
Console.WriteLine("\n");
Console.WriteLine("遍历 key");
foreach (int key in dicPerson.Keys)
{
string id = "用户ID:" + key;
string str = string.Format("name:{0} age:{1}", dicPerson[key].name, dicPerson[key].age);
Console.WriteLine(id + "\t" + str);
}
//遍历value
Console.WriteLine("\n");
Console.WriteLine("遍历 value");
foreach (Person value in dicPerson.Values)
{
string str = string.Format("name:{0} age:{1}", value.name, value.age);
Console.WriteLine(str);
}
//遍历字典
Console.WriteLine("\n");
Console.WriteLine("遍历字典");
foreach (KeyValuePair<int, Person> kvp in dicPerson)
{
string str = string.Format("key:{0}/name:{1}/age:{2}", kvp.Key, kvp.Value.name, kvp.Value.age);
Console.WriteLine(str);
}
// 删除元素
Console.WriteLine("\n");
Console.WriteLine("删除元素");
if (dicPerson.ContainsKey(1)) //如果存在
dicPerson.Remove(1);
foreach (Person value in dicPerson.Values)
{
string str = string.Format("name:{0} age:{1}", value.name, value.age);
Console.WriteLine(str);
}
//清除所有的元素
dicPerson.Clear();
Console.Read();
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApp9
{
class Program
{
public static int Asc(string s)
{
if(s.Length == 1)
{
ASCIIEncoding a = new ASCIIEncoding();
int b = (int)a.GetBytes(s)[0];
return b;
}
else
{
throw new Exception("String is not vaild");
}
}
public static char Cha(string s)
{
int a = int.Parse(s);
if(a >=0 && a <= 255)
{
char c = (char)a;
return c;
}
else
{
throw new Exception("String is not vaild");
}
}
static void PrintDic(Dictionary<char,int> dict)
{
foreach(var pair in dict)
{
Console.WriteLine(pair.Key + " " + pair.Value);
}
}
static void Main(string[] args)
{
Dictionary <char,int> dicA= new Dictionary<char, int>();
Random r = new Random();
string a =$"a";
//char a = 'a';
int asc = Asc(a);
for (int i = 0; i < 26; i++)
{
dicA.Add((char)asc, r.Next(1, 10));
asc++;
}
PrintDic(dicA);
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApp9
{
class Program
{
static void InitDic(Dictionary<char, int> dic)
{
Random r = new Random();
string a = $"a";
int asc = Asc(a);
for (int i = 0; i < 26; i++)
{
dic.Add((char)asc, r.Next(1, 10));
asc++;
}
}
public static int Asc(string s)
{
if(s.Length == 1)
{
ASCIIEncoding a = new ASCIIEncoding();
int b = (int)a.GetBytes(s)[0];
return b;
}
else
{
throw new Exception("String is not vaild");
}
}
public static char Cha(string s)
{
int a = int.Parse(s);
if(a >=0 && a <= 255)
{
char c = (char)a;
return c;
}
else
{
throw new Exception("String is not vaild");
}
}
static void PrintDic(Dictionary<char,int> dict)
{
foreach(var pair in dict)
{
Console.WriteLine(pair.Key + " " + pair.Value);
}
}
static void Deleven(Dictionary<char,int> dict)
{
List<char> L = new List<char>();
foreach(var pair in dict)
{
if (pair.Value % 2 == 0)
{
L.Add(pair.Key);
}
}
foreach(var key in L)
{
dict.Remove(key);
}
}
static void CombineAB(Dictionary<char, int> DA, Dictionary<char, int> DB, Dictionary<char, int> DC)
{
foreach (var pair in DA)
{
DC[pair.Key] = pair.Value;
}
foreach (var pair in DB)
{
if (!DA.ContainsKey(pair.Key))
{
DC[pair.Key] = pair.Value;
}
}
}
static void CombineAB2(Dictionary<char, int> DA, Dictionary<char, int> DB, Dictionary<char, int> DC)
{
foreach (var pair in DB)
{
DC[pair.Key] = pair.Value;
}
foreach (var pair in DA)
{
DC[pair.Key] = pair.Value;
}
}
static void Main(string[] args)
{
Dictionary<char, int> dicA = new Dictionary<char, int>();
InitDic(dicA);
PrintDic(dicA);
Console.WriteLine();
Deleven(dicA);
PrintDic(dicA);
Console.WriteLine();
Dictionary<char, int> dicB = new Dictionary<char, int>();
InitDic(dicB);
PrintDic(dicB);
Console.WriteLine();
Deleven(dicB);
PrintDic(dicB);
Console.WriteLine();
Dictionary<char, int> dicC = new Dictionary<char, int>();
CombineAB(dicA,dicB,dicC);
//或者 CombineAB2(dicA,dicB,dicC);
PrintDic(dicC);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApp10
{
class Program
{
static string Randomxiang()
{
Random R = new Random();
string str = string.Empty;
string Rch = "ABCDEFGHIJKLMNOPQRSTUWVXYZ0123456789abcdefghijklmnopqrstuvwxyz";
//string[] Rch = new string[R.Next(0, 10000)];
for (int i = 0; i < 2; i++)
{
str += Rch[R.Next(Rch.Length)];
}
return str;
}
static string[] Randomstr(int length)
{
string[] strs = new string[length];
for (int i = 0; i<length; i++)
{
strs[i] = Randomxiang();
}
return strs;
}
static void PrintString(string[] str)
{
for(int i = 0; i < str.Length; i++)
{
Console.Write(str[i]);
Console.Write(" ");
}
}
static void PrintDic(Dictionary<string,string> dict)
{
foreach(var pair in dict)
{
Console.WriteLine("("+pair.Key + " " + pair.Value+")");
}
}
static int Input()
{
int s = 0;
while (true)
{
if (int.TryParse(Console.ReadLine(), out s))
{
return s;
}
else
{
Console.WriteLine("请重新输入");
}
}
}
static void Main(string[] args)
{
int n = int.Parse(Console.ReadLine());
string[] str = Randomstr(n);
//PrintString(str);
Dictionary<string, string> D1 = new Dictionary<string, string>();
for (int i = 0; i < n; i += 2)
{
D1.Add(str[i], str[i + 1]);
}
PrintDic(D1);
}
}
}