C#中char[]与string之间的转换
string 转换成 Char[]
string ss = "abcdefg";
char[] cc = ss.ToCharArray();
Char[] 转换成string
string s = new string(cc);
此外,byte[] 与 string 之间的装换
byte[] bb = Encoding.UTF8.GetBytes(ss);
string s = Encoding.UTF8.GetString(bb);
我们也可以利用 StringBuilder 来进行数组 与 string 间的转换
using System.Text;
StringBuilder sb = new StringBuilder();
foreach(char c in cc)
sb.Append(c);
string s = sb.ToString();
///Object.ReferenceEquals(Object, Object) Method
如果 objA
是与 objB
相同的实例,或两者均为 null,则为 true
;否则为 false
。
object o = null; object p = null; object q = new Object(); Console.WriteLine(Object.ReferenceEquals(o, p)); p = q; Console.WriteLine(Object.ReferenceEquals(p, q)); Console.WriteLine(Object.ReferenceEquals(o, p));
/* This code produces the following output. True True False */
注解
与不同Equals方法和相等运算符ReferenceEquals不能重写方法。 因此,如果你想要测试两个对象是否相等的引用而您不确定的实现Equals
方法时,可以调用ReferenceEquals方法。
但是,返回值的ReferenceEquals方法似乎存在异常中这两种方案:
-
比较值类型。 如果
objA
并objB
是值类型,它们装箱传递到之前ReferenceEquals方法。 这意味着,如果这两个objA
并objB
表示的值类型的相同实例ReferenceEquals方法不过返回false
-
int int1 = 3; Console.WriteLine(Object.ReferenceEquals(int1, int1)); Console.WriteLine(int1.GetType().IsValueType);
-
// The example displays the following output: // False // True
-
当比较字符串。 如果
objA
并objB
都是字符串,ReferenceEquals方法将返回true
如果字符串暂留。 它不会执行测试的值相等。 在以下示例中,s1
和s2
因为它们是单一的暂存字符串的两个实例是否相等。 但是,s3
和s4
是否不相等,因为尽管它们都具有相同的字符串值,但该字符串未被暂存。 -
String s1 = "String1"; String s2 = "String1"; Console.WriteLine("s1 = s2: {0}", Object.ReferenceEquals(s1, s2)); Console.WriteLine("{0} interned: {1}", s1, String.IsNullOrEmpty(String.IsInterned(s1)) ? "No" : "Yes"); String suffix = "A"; String s3 = "String" + suffix; String s4 = "String" + suffix; Console.WriteLine("s3 = s4: {0}", Object.ReferenceEquals(s3, s4)); Console.WriteLine("{0} interned: {1}", s3, String.IsNullOrEmpty(String.IsInterned(s3)) ? "No" : "Yes");
-
// The example displays the following output: // s1 = s2: True // String1 interned: Yes // s3 = s4: False // StringA interned: No
c# 类型 信息/
//byte类型从0 -> 255。
byte xb = 1; //整型1隐式转换成byte类型.如果整数大于255,则产生异常(错误)
Console.WriteLine(xb.GetType());
// output System.Byte
Console.WriteLine(sizeof(System.Byte)); // 1
//doesn't work
//byte xb1 = 256;
short xs = 2;
Console.WriteLine(xs.GetType());
//output System.Int16
Console.WriteLine(sizeof(System.Int16));// 2
int xi = 1;
Console.WriteLine(xi.GetType());
//output System.Int32
Console.WriteLine(sizeof(System.Int32));// 4
long xl = 3;
Console.WriteLine(xl.GetType());
//output System.Int64
Console.WriteLine(sizeof(System.Int64));// 8
//doesn't work , ushort,uint,ulong表示无符号的short,int,long,普通点说,就是没有负数,只有正数和0
//ushort xus = -1;
ushort xus = 1;
Console.WriteLine(xus.GetType());
//output System.UInt16
Console.WriteLine(sizeof(System.UInt16));// 2
uint xui = 2;
Console.WriteLine(xui.GetType());
//output System.UInt32
Console.WriteLine(sizeof(System.UInt32));// 4
ulong xul = 3;
Console.WriteLine(xul.GetType());
//output System.UInt64
Console.WriteLine(sizeof(System.UInt64));// 8
decimal xd = 2; //整型2隐式转换成decimal类型
Console.WriteLine(xd.GetType());
//output System.Decimal
Console.WriteLine(sizeof(System.Decimal)); // 16
Console.WriteLine(xd);// 2
float xf = 2f;
Console.WriteLine(xf.GetType());
//output System.Single -- 单精度
Console.WriteLine(sizeof(System.Single));// 4
double xdo = 2; // 整型2隐式转换成double类型
Console.WriteLine(xdo.GetType());
//output System.Double -- 双精度
Console.WriteLine(sizeof(System.Double));// 8
Console.WriteLine(xdo);// 2
Console.ReadLine();
/
nt32 lTemp = Convert.ToInt32("0xa10b", 16);
这样的话,就将0xa10b这个十六制形式的字符串转为十进制的数字了
十进制的数字了
C++ int型进行高16位和低16位的操作
int l16 =int 类型 >> 16;
int h16 = int 类型 & 0x0000ffff;
int a = 1254;
int a1 = (a & 0x00FF0000);
int a11 = a1 >> 16;
int a2 = (a & 0x0000FF00);
int a22 = a2 >> 8;
int a3 = a & 0x000000FF;
var a6 = Convert.ToString(a, 2);
int g = 42;
int g1 = g >> 1;
int g11 = g1 & 0x1;
int g2 = g >> 2;
int g21 = g2 & 0x1;
int g3 = g >> 3;
int g31 = g3 & 0x1;
int g4 = g >> 4;
int g41 = g4 & 0x1;
int g5 = g >> 5;
int g51 = g5 & 0x1;
int g6 = g >> 6;
int g61 = g6 & 0x1;
//字符串转ASCII码
string str = "abcd";
byte[] bytetest = System.Text.Encoding.Default.GetBytes(str);
//字符转ASCII码:
public static int Asc(string character)
{
if (character.Length == 1)
{
System.Text.ASCIIEncoding asciiEncoding = new System.Text.ASCIIEncoding();
int intAsciiCode = (int)asciiEncoding.GetBytes(character)[0];
return intAsciiCode;
}
else
{
throw new Exception("Character is not valid.");
}
}
ASCII转字符串
string str = char.ConvertFromUtf32(65)
//ASCII码转字符:
public static string Chr(int asciiCode)
{
if (asciiCode >= 0&& asciiCode <= 255)
{
System.Text.ASCIEncoding asciiEncoding = new System.TextASCIlEncoding();
byte[]byteArray = new byte[]{(byte)asciiCode };
string strCharacter = asciiEncoding.GetString(byteArray);
return strCharacter;
}
else
{
throw new Exception("ASCII Code is not valid.");
}
}
用C#怎么判定一个数是整数还是小数?
我的方法是把一个变量定义成float,如下:
float a;
if (a - (int)a != 0); ///这样可以确定是小数;
方法2:
float a;
if (a % 1 != 0); ///这样也行;
可是这些方法都不能判定诸如1.0 2.0 之类的小数
解决:float a;
......//运算对a赋值
string str_a=Convert.ToString(a);
int dot=str_a.IndexOf(".");
string substr=a.Substring(dot+1);
bool hasnotzerochar=false;//记录是否小数点后存在不为0的字符
for (i=0;i<substr.Length();i++)
{
if (substr[i]!="0")
{
hasnotzerochar=true;
}
}
这样,你只需要判断hasnotzerochar是否为true就可以知道了
浮点数相等的判断(由于浮点数有误差, 所以判断的时候最好不要用等号,尤其是计算出来的结果)
if (Mathf.Approximately(1.0, 10.0/10.0))
print ("same");
可以使用如下方式:
1. String.Compare(str1, str2) == 0 或者 str1.CompareTo(str2) == 0
2. str1.Equals(str2) 或者 String.Equals(str1, str2)
这种方式的话,需要注意null的情况
3. str1 == str2
这是因为String.Equals方法已经重载了,和==是一样的效果。