C
C#ā€¢2y ago
gododo

āœ… Output not correct

Im doing a Client/server program the program will give out in output if the user is a minor or not the issue is that it always says that the user is an adult idk what im doing wrong
49 Replies
gododo
gododoOPā€¢2y ago
numeroricevuto is the number recieved from the client
Relevant
Relevantā€¢2y ago
I'll check out BitConverter but that sounds wrong nevermind, seems fine
gododo
gododoOPā€¢2y ago
oh
Relevant
Relevantā€¢2y ago
Can you output your byte array to console to make sure it looks how you expect? There might be some header values or something you weren't expecting
gododo
gododoOPā€¢2y ago
idek how to do it im very bad at coding
Relevant
Relevantā€¢2y ago
Oh wait, I think I know what it is
gododo
gododoOPā€¢2y ago
Hmm
Relevant
Relevantā€¢2y ago
Your byte array is 256 bytes, and if you're reading just a single int, that's just the first 4 bytes. So then you have a bunch of 0s at the end. So I'm assuming that would convert it to a giant number Can you set a break point and step through to see what the value of numeroricevuto is?
gododo
gododoOPā€¢2y ago
i can try
Relevant
Relevantā€¢2y ago
Meh, that's not it either, looks like BitConverter.ToInt32 just takes the first 4 bytes from whatever startIndex you give it, and you used 0, so that is probably correct, too. Anyways, to troubleshoot, just check your buffer after the read then check the int after the conversion
gododo
gododoOPā€¢2y ago
i wrote console write line and the number the numer that came out was 13k lol instead of 16
Relevant
Relevantā€¢2y ago
What values are in the byte array? Console.WriteLine(string.Join(", ", datiricevuti));
gododo
gododoOPā€¢2y ago
do i need to copy and paste that
Relevant
Relevantā€¢2y ago
yes put it after the read
gododo
gododoOPā€¢2y ago
alr ok
Relevant
Relevantā€¢2y ago
Might be cleaner adding a .Take(4) Console.WriteLine(string.Join(", ", datiricevuti.Take(4))); So you don't see a ton of 0s
gododo
gododoOPā€¢2y ago
did this and all of this came out
gododo
gododoOPā€¢2y ago
oh the number i used was 15
Relevant
Relevantā€¢2y ago
So your first 4 bytes are 49 53 0 0
gododo
gododoOPā€¢2y ago
ok
Relevant
Relevantā€¢2y ago
So that would be a big number
gododo
gododoOPā€¢2y ago
yeah when i used console.writeline the number that came out was 13.000 and not 16
Relevant
Relevantā€¢2y ago
How is this data being sent? Oh wait... I know what it is haha
gododo
gododoOPā€¢2y ago
oh what
Relevant
Relevantā€¢2y ago
making sure I'm right. 1 sec
gododo
gododoOPā€¢2y ago
alr
Relevant
Relevantā€¢2y ago
Yeah, it's sending it as text. 49 is the ascii code for 1 and 53 is the ascii code for 5 so that says 15 But you're converting it as if it's a numeric value
gododo
gododoOPā€¢2y ago
oh so what comand do i need to use now
Relevant
Relevantā€¢2y ago
You could do something like numeroricevuto = (int)Encoding.UTF8.GetString(datiricevuti, 0, 4); Not sure if that would work. And even if it does, it would break if it's not an int. So better checking and parsing to int would be better
gododo
gododoOPā€¢2y ago
ill try that it says i cant convert string to int hm
Relevant
Relevantā€¢2y ago
1 sec, I'm playing around with it now
gododo
gododoOPā€¢2y ago
ok
Relevant
Relevantā€¢2y ago
yeah, tryparse would be better anyways
gododo
gododoOPā€¢2y ago
where should i put this tryparse
Relevant
Relevantā€¢2y ago
if (!int.TryParse(Encoding.UTF8.GetString(datiricevuti, 0, 4), out numeroricevuto))
{
Console.WriteLine("Failed to parse");
}
if (!int.TryParse(Encoding.UTF8.GetString(datiricevuti, 0, 4), out numeroricevuto))
{
Console.WriteLine("Failed to parse");
}
Something like that should work
gododo
gododoOPā€¢2y ago
should i write Byte instead of bytes where theres the error
Relevant
Relevantā€¢2y ago
oops sorry, that was from my text code. I'll edit
gododo
gododoOPā€¢2y ago
šŸ‘
Relevant
Relevantā€¢2y ago
There ya go
gododo
gododoOPā€¢2y ago
yooo it works thank you man šŸ™
Anchy
Anchyā€¢2y ago
the issue actually lies in your client and not your server you should just send the bytes for the int, but it sounds like you are sending a string over it doesn't really make sense to send over bytes of a string just to convert it back into a string and then an int I don't see where you have shared the client code
gododo
gododoOPā€¢2y ago
the program works now but i didnt send the client because the issue mainly was in the server i can show you the client if u want
gododo
gododoOPā€¢2y ago
this was my windows form client
Anchy
Anchyā€¢2y ago
the issue definitely looks like it lies in the client
Relevant
Relevantā€¢2y ago
Yeah, wouldn't have been an issue if it was sent as an int
Anchy
Anchyā€¢2y ago
you want to actually parse the textbox as an int and convert it for transport as a byte array so what you did to fix it but on the client side instead
gododo
gododoOPā€¢2y ago
oh i see next time ill do that thank you for helping anyway
Anchy
Anchyā€¢2y ago
do it how you like but I thought I would just weigh in on what the actual issue was šŸ˜„
gododo
gododoOPā€¢2y ago
šŸ‘
Want results from more Discord servers?
Add your server