C#中char[]与string之间的转换;byte[]与string之间的转化
(1)C#中char[]与string互相转换的写法:
string 转换成 Char[]
string ss="abcdefg";
char[] cc=ss.ToCharArray();
Char[] 转换成string
string s=new string(cc);
--------------------------------------------------
(2) C#中byte[]与string互相转换的写法:
byte[] 转换成string
byte[] bb=Encoding.UTF8.GetBytes(ss);
string s=Encoding.UTF8.GetString(bb);
string 转换为byte[]
byte[] bytes;
string str = "abc";
System.Text.ASCIIEncoding ascill = new ASCIIEncoding();
bytes = ascill.GetBytes(str);
string 转换成 Char[]
string ss="abcdefg";
char[] cc=ss.ToCharArray();
Char[] 转换成string
string s=new string(cc);
--------------------------------------------------
(2) C#中byte[]与string互相转换的写法:
byte[] 转换成string
byte[] bb=Encoding.UTF8.GetBytes(ss);
string s=Encoding.UTF8.GetString(bb);
string 转换为byte[]
byte[] bytes;
string str = "abc";
System.Text.ASCIIEncoding ascill = new ASCIIEncoding();
bytes = ascill.GetBytes(str);
(3)将一整数逆序,如987654321变为123456789。
//方法一:倒序循环
int[] intArray = { 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };
for (int i = intArray.Length - 1; i >= 0; i--)
{
//intArray[i];
}
//方法二:使用反转函数Reverse
int[] newArrary = intArray.Reverse().ToArray();
//方法三:C#中char[]与string互相转换的写法:
string strNum = "9876543210";
char[] reversed = strNum.Reverse().ToArray();
string newStrNum = new string(reversed);
//方法一:倒序循环
int[] intArray = { 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };
for (int i = intArray.Length - 1; i >= 0; i--)
{
//intArray[i];
}
//方法二:使用反转函数Reverse
int[] newArrary = intArray.Reverse().ToArray();
//方法三:C#中char[]与string互相转换的写法:
string strNum = "9876543210";
char[] reversed = strNum.Reverse().ToArray();
string newStrNum = new string(reversed);
(4)、一、数组(Array)转换为枚举(Enum)类型 enum TypeName { a, b, c } 方法: string[] names = new string[] { "a", "b", "c" }; TypeName[] typeNames = names.Select(t => (TypeName)Enum.Parse(typeof(TypeName), t)).ToArray(); 字符串转换为枚举类型: TypeNametype=(TypeName)Enum.Parse(typeof(TypeName), 'a') 二、枚举(Enum)类型转换为数组(Array) public enum EmploymentTypeEnum { FullTime, Apply, PartTime, } string[] tmp = Enum.GetNames(typeof(EmploymentTypeEnum));
作者:阿笨
【官方QQ一群:跟着阿笨一起玩NET(已满)】:422315558
【官方QQ二群:跟着阿笨一起玩C#(已满)】:574187616
【官方QQ三群:跟着阿笨一起玩ASP.NET(已满)】:967920586
【官方QQ四群:Asp.Net Core跨平台技术开发(可加入)】:829227829
【官方QQ五群:.NET Core跨平台开发技术(可加入)】:647639415
【网易云课堂】:https://study.163.com/provider/2544628/index.htm?share=2&shareId=2544628
【腾讯课堂】:https://abennet.ke.qq.com
【51CTO学院】:https://edu.51cto.com/sd/66c64
【微信公众号】:微信搜索:跟着阿笨一起玩NET