actionscript 3 - ByTeArray AS3 function from C# function help needed -
i noob bytearray use in as3 , need help. trying reproduce "header" function in as3 returning bytearray c# function (code provided bellow). can point me in right direction?
i got point , not sure how continue:
private function header():bytearray { var header:bytearray = new bytearray(); header.writeutfbytes("a"); header.writeutfbytes("b"); header.writeutfbytes("c"); header.writeutfbytes("d"); ... }
thank you
private enum packettype { pthelo = 0x01, ptbye = 0x02, ptbutton = 0x03, ptmouse = 0x04, } private const byte bv = 2; private const byte mv = 0; private byte[] header(packettype packettype, int numberofpackets, int currentpacket, int payloadsize) { var header = new byte[headersize]; header[0] = (byte)'a'; header[1] = (byte)'b'; header[2] = (byte)'c'; header[3] = (byte)'d'; header[4] = bv; header[5] = mv; if (currentpacket == 1) { header[6] = (byte)(((ushort)packettype & 0xff00) >> 8); header[7] = (byte)((ushort)packettype & 0x00ff); } else { header[6] = ((ushort)packettype.ptblob & 0xff00) >> 8; header[7] = (ushort)packettype.ptblob & 0x00ff; } header[8] = (byte)((currentpacket & 0xff000000) >> 24); header[9] = (byte)((currentpacket & 0x00ff0000) >> 16); header[10] = (byte)((currentpacket & 0x0000ff00) >> 8); header[11] = (byte)(currentpacket & 0x000000ff); header[12] = (byte)((numberofpackets & 0xff000000) >> 24); header[13] = (byte)((numberofpackets & 0x00ff0000) >> 16); header[14] = (byte)((numberofpackets & 0x0000ff00) >> 8); header[15] = (byte)(numberofpackets & 0x000000ff); header[16] = (byte)((payloadsize & 0xff00) >> 8); header[17] = (byte)(payloadsize & 0x00ff); return header; }
the rest of header function can use same array indices , bit arithmetic c# version (removing casts). might simpler avoid arithmetic:
header.endian = endian.big_endian; // important writing multibyte below header.writeutfbytes('abcd'); header.writebyte(bv); header.writebyte(mv); if (...) { header.writeshort(packettype); } else { header.writeshort(ptblob); // ptblob constant defined elsewhere } header.writeint(currentpacket); ...
using byte arithmetic:
header.writebyte((packettype & 0xff00) >> 8); header.writebyte(packettype & 0x00ff);
since above writing 2 bytes packettype
, it's equivalent big-endian writeshort()
.
Comments
Post a Comment