看看这个成不成...^_*
static void Main(string[] args)
{
//
En();
//
}
static void En()
{
PacketHeader ph = new PacketHeader();
ph.iToB4(Int32.MaxValue);
byte [] b4 = new byte[4]{98,98,0,98}; //数组按照从右向左排列,即现在是98|0|98|98
ph.b4ToI(b4);
ph.bToC(b4);
ph.cToB(new char[8]{'a','b','1','0','c','d','e','f'});
//
}
private byte[] iToB4(int i)
{
Console.WriteLine("int convert to byte[4]:");
int[] i_Array = new int[1]{i};
byte[] b_Array = new byte[4];
BitArray ba_I = new BitArray(i_Array);
ba_I.CopyTo(b_Array, 0);
for (int x = 0;
x != b_Array.Length;
x ++)
{
Console.WriteLine(b_Array[x]);
}
return b_Array;
}
private int b4ToI(byte[] b_Array)
{
Console.WriteLine("byte[4] convert to int:");
int[] i_Array = new int[1];
BitArray ba_Byte = new BitArray(b_Array);
ba_Byte.CopyTo(i_Array, 0);
Console.WriteLine(i_Array[0]);
return i_Array[0];
}
private char[] bToC(byte[] b_Array)
{
Console.WriteLine("byte[] convert to char[]:");
char[] c_Array = new char[b_Array.Length];
for (int i = 0;
i != b_Array.Length;
i ++)
{
c_Array = (char)b_Array;
}
Console.WriteLine(c_Array);
return c_Array;
}
private byte [] cToB(Char[] c_Array)
{
Console.WriteLine("char[] convert to byte[]:");
byte[] b_Array = new byte[c_Array.Length];
for (int i = 0;
i != c_Array.Length;
i ++)
{
b_Array = (byte)c_Array;
Console.WriteLine(b_Array);
}
return b_Array;
}