怎样才能用收到byte[]数组添充这些字段(40分)

  • 主题发起人 lirenzhao
  • 开始时间
L

lirenzhao

Unregistered / Unconfirmed
GUEST, unregistred user!
我有这样一个类
PacketHeader
{
public uint version;
public uchar password[8];
public ulong length;
}
怎样才能用收到byte[]数组添充这些字段》?
 
你的域就有问题:没有uchar类型!
说说你想实现的功能吧!
 
class PacketHeader
{
public uint version;
public char[] password;
public ulong length;

/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
Entrance();
//
}
static void Entrance()
{
byte [] b = new byte[] {1,3};
PacketHeader ph = new PacketHeader();
ph.Fill(b);
Console.WriteLine("version={0},password={1},length={2}",ph.version,ph.password[0],ph.length);
}
public void Fill(byte[] b)
{
string s_Temp = b[1].ToString();
version = b[0];
password = new char[s_Temp.Length];
for (int i = 0;
i != s_Temp.Length;
i ++)
{
password = s_Temp;
}
length = (ulong)b.Length;
}
}
 
但是第一个字段是整数应该是4个字节啊?
因为Socket通信,我读取的byte[],然后我解析后返回还要在添成byte[]发送
我要的第一个方法
int i = 100100;
把i变成byte[4]
....................
我要的第二个功能
然后把 整数变成 byte[]
byte[] b = new Encoding.ASCII.GetBytes(i.toString());
//错误的做法
 
其实就是把整形和byte[]之间的转变
 
你的意思是不是想使用int中的每个bit?
如果是可以用BitArray类。
public static void iTob(int i)
{
int[] i_Array = new int[1];
i_Array[0] = i;
BitArray ba = new BitArray(i_Array);
for (int x = 0;
x != ba.Count;
x ++)
{
if (ba[x])
{
Console.Write("1");
}
else
{
Console.Write("0");
}
}
}
 
可是对方是c写的程序
我想握主要是想问
int <=> byte[4]
char[8] <=> byte[8]
的问题
 
你弄那黑8干什么?打台球吗?
int --> byte [4] :
static byte[] iTo(int i)
{
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;
}
其他的应该类似...
 
看看这个成不成...^_*
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;
}
 
如果我有这样的一个类该如何socket发送接受呢?
class Packetheader
{
uint version;
char[] password;
//8
word Command;
int bodyLength;
}
class Packet
{
Packetheader ph;
char[] body;
//不定长,根据包头的长度
}
对方是vc的客户端,我左服务器
 
1.发送数据最好用struct;
2.既然你做服务器那结构你来定,让对方按你的定义进行接收就OK了。
3.如何发送,要看你用的是什么协议。
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
454
import
I
顶部 底部