'c# convert byte[] to byte but keep value the same
So i have a program that communicates through tcp, the server was already done, but i don't understand how to convert the byte[] to byte but keeping the value the same.
public static List<byte[]> Separate(byte[] source, byte[] separator)
{
List<byte[]> list = new List<byte[]>();
int num = 0;
byte[] array;
for (int i = 0; i < source.Length; i++)
{
if (Equals(source, separator, i))
{
array = new byte[i - num];
Array.Copy(source, num, array, 0, array.Length);
list.Add(array);
num = i + separator.Length;
i += separator.Length - 1;
}
}
array = new byte[source.Length - num];
Array.Copy(source, num, array, 0, array.Length);
list.Add(array);
return list;
}
private static bool Equals(byte[] source, byte[] separator, int index)
{
for (int i = 0; i < separator.Length; i++)
{
if (index + i >= source.Length || source[index + i] != separator[i])
{
return false;
}
}
return true;
}
private static byte[] delimiter = Encoding.ASCII.GetBytes("<EOM>");
private static byte[] end = Encoding.ASCII.GetBytes("<EOF>");
public static IServerPacket BuildPacket(byte[] _data)
{
try
{
List<byte[]> list = new List<byte[]> { _data };
byte b = list[0].ToList().GetRange(0, 1)[0];
IServerPacket result = null;
List<byte> list2 = list[0].ToList();
list2.RemoveRange(0, 6);
list[0] = list2.ToArray();
list = Separate(Separate(list[0], end)[0], delimiter);
//printing list[0][0] here gives me 50 instead of 2
switch (b)
{
case 1:
result = new Start(1, list[0][0]);
break;
case 2:
result = new Stop();
break;
case 3:
result = new MouseEvnt(list[0][0], list[1][0], list[2], list[3], list[4][0]);
break;
case 4:
result = new KeyEvnt(list[0][0], list[1][0]);
break;
}
return result;
}
catch
{
}
return null;
}
what i'm sending with the server : 1<EOM>2<EOM>3<EOM>4<EOM>5<EOM>6<EOM> , printing list[0][0] will give me 50 instead of 2. tried everything but i just couldn't make it work..
Solution 1:[1]
You need to be MUCH more aware of the types you already have and resulting types of each expression within a statement. So much of the original code in this method was simply not doing anything productive at all, just to get back to the original value you already had. There's no reason for any of that code in the question to ever convert those byte arrays to List.
public static IServerPacket BuildPacket(byte[] _data)
{
byte b = _data[0];
IServerPacket result = null;
switch ((char)b)
{
case '1':
result = new Start(1, b);
break;
case '2':
result = new Stop();
break;
case '3':
result = new MouseEvnt(b, _data[1], _data[2], _data[3], _data[4]);
break;
case '4':
result = new KeyEvnt(b, _data[1]);
break;
}
return result;
}
Alternatively you can keep the ascii values, which might be more efficient:
public static IServerPacket BuildPacket(byte[] _data)
{
byte b = _data[0];
IServerPacket result = null;
switch (b)
{
case 49:
result = new Start(1, b);
break;
case 50:
result = new Stop();
break;
case 51:
result = new MouseEvnt(b, _data[1], _data[2], _data[3], _data[4]);
break;
case 52:
result = new KeyEvnt(b, _data[1]);
break;
}
return result;
}
Not sure on the event objects, since I can't see how they work, but I suspect this is pretty close.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
