Mar 2, 2022 01:39 PM
(This post was last modified: Mar 2, 2022 01:40 PM by Julius013.)
hello, so i have a project that communicates using tcp, (the client is taken from somewhere else) when the client receives the byte array packet, it somehow converts it to byte but keeps the value the same (not Unicode)
an example :
Code public static IServerPacket packet(byte[] data)
{
try
{
var list = new List<byte[]> { data };
byte b = list[0].ToList().GetRange(0, 1)[0]; //b will give me 49 here, i want it to give me 1 so it can go through the switch case
...
...
...
switch (b)
{
case 1:
...
break;
}
}
catch{ }
}
Don't want to convert it to char because it will break the whole thing plus a lot of stuff is more than a digit, plus i don't want to leave it as it is (unicode)
any help is appreciated!
Mar 2, 2022 02:14 PM
(This post was last modified: Mar 2, 2022 02:15 PM by 0x9F.)
(Mar 2, 2022 01:39 PM)Julius013 Wrote: hello, so i have a project that communicates using tcp, (the client is taken from somewhere else) when the client receives the byte array packet, it somehow converts it to byte but keeps the value the same (not Unicode)
an example :
Code public static IServerPacket packet(byte[] data)
{
try
{
var list = new List<byte[]> { data };
byte b = list[0].ToList().GetRange(0, 1)[0]; //b will give me 49 here, i want it to give me 1 so it can go through the switch case
...
...
...
switch (b)
{
case 1:
...
break;
}
}
catch{ }
}
Don't want to convert it to char because it will break the whole thing plus a lot of stuff is more than a digit, plus i don't want to leave it as it is (unicode)
any help is appreciated!
this will do the trick
Code list[0].ToList().GetRange(0, 1)[0] - '0';
However, why aren't you simplifying your code by working directly with your param 'data' ... ?
Because in the end of the day, according to your code, you're accessing data[0] and assigning the value to 'b'.
What about this:
Code public static IServerPacket packet(byte[] data)
{
try
{
int state = data[0] - '0';
switch (state)
{
case 1:
...
break;
}
}
catch { }
}
Note that this trick is only reliable with numerical input (0x30 to 0x39).
Mar 2, 2022 04:15 PM
(This post was last modified: Mar 2, 2022 04:16 PM by Julius013.)
(Mar 2, 2022 02:14 PM)0x9F Wrote: this will do the trick
Code list[0].ToList().GetRange(0, 1)[0] - '0';
However, why aren't you simplifying your code by working directly with your param 'data' ... ?
Because in the end of the day, according to your code, you're accessing data[0] and assigning the value to 'b'.
What about this:
Code public static IServerPacket packet(byte[] data)
{
try
{
int state = data[0] - '0';
switch (state)
{
case 1:
...
break;
}
}
catch { }
}
Note that this trick is only reliable with numerical input (0x30 to 0x39).
hello, the thing is that the variable b could be anything between 1-9 (depends of what the server has sent)
because the program is doing a lot of stuff afterwards (splitting the list, sending a part of the list as byte or byte[] ( list[0][0], list[2] )), so because the program is intended to work as it is, the server is supposed to send a different value that i'm not aware of.
(the client is not made by me, i'm only trying to create the server based off the client)
(Mar 2, 2022 04:15 PM)Julius013 Wrote: hello, the thing is that the variable b could be anything between 1-9 (depends of what the server has sent)
because the program is doing a lot of stuff afterwards (splitting the list, sending a part of the list as byte or byte[] ( list[0][0], list[2] )), so because the program is intended to work as it is, the server is supposed to send a different value that i'm not aware of.
(the client is not made by me, i'm only trying to create the server based off the client)
If I understand you correctly, b (or data[0] in this special case) will always be a numeric value between 1-9?
Then this solution should actually do the trick.
Code byte b = list[0].ToList().GetRange(0, 1)[0] - '0';
Using simple maths, substracting '0' (0x30) from b (which is something between 1-9) will return you the actual numeric value in b.
Example:
'3' (Hex: 0x33/Dec: 51) - '0' (Hex: 0x30/Dec: 48)
Take a look at the ASCII-table:
ASCII-Table
(Mar 2, 2022 04:38 PM)0x9F Wrote: If I understand you correctly, b (or data[0] in this special case) will always be a numeric value between 1-9?
Then this solution should actually do the trick.
Code byte b = list[0].ToList().GetRange(0, 1)[0] - '0';
Using simple maths, substracting '0' (0x30) from b (which is something between 1-9) will return you the actual numeric value in b.
Example:
'3' (Hex: 0x33/Dec: 51) - '0' (Hex: 0x30/Dec: 48)
Take a look at the ASCII-table:
you're right, this trick will work in most cases but sadly after checking everything out there's also numbers above 10, for the variable b if fits fine but not for the rest, the client cannot be modified as it is, the server has to be modified
|