一、进制之间转换
//十进制转二进制
Console.WriteLine(Convert.ToString(69, 2));
//十进制转八进制
Console.WriteLine(Convert.ToString(69, 8));
//十进制转十六进制
Console.WriteLine(Convert.ToString(69, 16));
//二进制转十进制
Console.WriteLine(Convert.ToInt32(”100111101〃, 2));
//八进制转十进制
Console.WriteLine(Convert.ToInt32(”76〃, 8));
//十六进制转十进制
Console.WriteLine(Convert.ToInt32(”FF”, 16));
二、字节与字符串、整数等转换
1、字符串与整数的相互转换
//整数转字符串
int a = 33;
string str_a = Convert.ToString(a);
Console.WriteLine(Convert.ToString(str_a));
//字符串转整数
string str = "444";
int i_str = Convert.ToInt32(str);
Console.WriteLine(i_str);
Console.ReadLine();
2、 字符串与字节的相互转换
//string类型转成byte[]:
byte[] byteArray = System.Text.Encoding.Default.GetBytes ( str );
//byte[]转成string:
string str = System.Text.Encoding.Default.GetString ( byteArray );
3、Int整数类型才能进行按位与运算
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace test
{
class Program
{
static void Main(string[] args)
{
//整数才能进行或操作
int a = 0x21;
int b = 0x22;
int c = a | b;
byte[] aa = new byte[4] { 0x21, 0, 0, 0 };
byte[] bb = new byte[4] { 0x22, 0, 0, 0 };
byte[] cc = new byte[0];
//cc = aa | bb;//字节之间不能进行或操作。
//所以把自己转换为整数,最后才进行或操作。
int aa1 = System.BitConverter.ToInt32(aa,0); //要注意aa bb 必须是4个字节。不然没办法转换为 int类型
int bb1 = System.BitConverter.ToInt32(bb, 0);
//你可以验证一下,一个int整数转成byte,就是四个字节。因为int默认是4字节。
int num = 4;
byte[] bytes = BitConverter.GetBytes(num);//将int32转换为字节数组
Console.WriteLine( (aa1| bb1) );
Console.WriteLine((aa1 | bb1));//
//当然,你也可以建立byte[2]对应Int16
byte[] aa2 = new byte[2] { 0x21, 0 };
Int16 num2 = System.BitConverter.ToInt16(aa2,0);//Int16不要写成int16,因为int16不存在,只有int。
Console.WriteLine(num2);
//以下实现提取某一位的操作,将与操作。
int temp1 = 0x05;//0B00000101
int temp2 = 0x09;//0B00001001
int checkBit3 = 0x04;//0B0000100 提取第三位
int checkBit8 = 0x08;//0B0000100 提取第四位
int result3 = temp1 & checkBit3;
int result4 = temp2 & checkBit8;
Console.WriteLine(result3);
Console.WriteLine(result4);
Console.WriteLine(0x13);
}
}
}
4、String字符串类型转字符数组 List<char>
5、将一个较大的整数,转成字节的高8位、低八位。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace test
{
class Program
{
static void Main(string[] args)
{
//int iContextFlagLong = 2;
//int iContextByteLong = 259;
//byte[] byt_iContextByteLong = new byte[iContextFlagLong];//用来表示内容的长度,2B字节保存。
//byt_iContextByteLong = BitConverter.GetBytes(iContextByteLong);
//字符长度字节 1 Byte
int iContextFlagLong = 2;
int iContextByteLong;
byte[] byt_StrContextLong = new byte[iContextFlagLong];//用来表示内容的长度,2B字节保存。
byte[] byt_StrContextLongVar = new byte[4];
iContextByteLong = 259;
byt_StrContextLongVar = BitConverter.GetBytes(iContextByteLong);
byt_StrContextLong[0] = byt_StrContextLongVar[0];//低八位
byt_StrContextLong[1] = byt_StrContextLongVar[1];//高八位
//将字符长度字节与内容字节合并
List<byte> listButter = new List<byte>();
byte[] byt_SumProtocolByteLong = new byte[1] { 23 };
listButter.AddRange(byt_StrContextLong);
listButter.AddRange(byt_SumProtocolByteLong);
}
}
}
6、利用AddRange拷贝到另一数组中:
三、C#位操作运算
注意啊,只有整型或字符型才能进行与或等运算,如图所示:
注意点:与操作,起码有一个int类型,才能进行与操作。
1、两个都是Int16类型,不能进行与操作
Int16 Int16_AndFlag = 0x01;
Int16 SendFlagTemp = 0x10;
Int16 a = 0;
a = (SendFlagTemp & Int16_AndFlag);
2、一个Int16类型 可以和另一个int类型进行与,强制转为int类型(按位与提取字节的某一位)
四、字符串操作
4.1、Replace拷贝字符串,去掉源字符的结束符\0(因为源字符串可能包含了很多个结束符,这些结束符可能在不同的位置上):
其实,你可以用C# Replace函数,实现任何对字符或字符串的替换。
4.2、Substring拷贝或截取部分字符串
int iPropertyIndex = strSingleUser.IndexOf('*');
string strProperty = strSingleUser.Substring(0, iPropertyIndex);
4.3、Remove移除字符串(注意移除后返回给自己)
int iPropertyIndex = strSingleUser.IndexOf('*');
string strProperty = strSingleUser.Substring(0, iPropertyIndex);
strSingleUser = strSingleUser.Remove(0, iPropertyIndex + 1);
https://blog.csdn.net/fxhflower/article/details/7220780
https://www.cnblogs.com/zgqys1980/archive/2010/05/31/1748404.html