C
C#ā€¢17mo 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
gododoā€¢17mo ago
numeroricevuto is the number recieved from the client
Relevant
Relevantā€¢17mo ago
I'll check out BitConverter but that sounds wrong nevermind, seems fine
gododo
gododoā€¢17mo ago
oh
Relevant
Relevantā€¢17mo 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
gododoā€¢17mo ago
idek how to do it im very bad at coding
Relevant
Relevantā€¢17mo ago
Oh wait, I think I know what it is
gododo
gododoā€¢17mo ago
Hmm
Relevant
Relevantā€¢17mo 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
gododoā€¢17mo ago
i can try
Relevant
Relevantā€¢17mo 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
gododoā€¢17mo ago
i wrote console write line and the number the numer that came out was 13k lol instead of 16
Relevant
Relevantā€¢17mo ago
What values are in the byte array? Console.WriteLine(string.Join(", ", datiricevuti));
gododo
gododoā€¢17mo ago
do i need to copy and paste that
Relevant
Relevantā€¢17mo ago
yes put it after the read
gododo
gododoā€¢17mo ago
alr ok
Relevant
Relevantā€¢17mo 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
gododoā€¢17mo ago
did this and all of this came out
gododo
gododoā€¢17mo ago
oh the number i used was 15
Relevant
Relevantā€¢17mo ago
So your first 4 bytes are 49 53 0 0
gododo
gododoā€¢17mo ago
ok
Relevant
Relevantā€¢17mo ago
So that would be a big number
gododo
gododoā€¢17mo ago
yeah when i used console.writeline the number that came out was 13.000 and not 16
Relevant
Relevantā€¢17mo ago
How is this data being sent? Oh wait... I know what it is haha
gododo
gododoā€¢17mo ago
oh what
Relevant
Relevantā€¢17mo ago
making sure I'm right. 1 sec
gododo
gododoā€¢17mo ago
alr
Relevant
Relevantā€¢17mo 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
gododoā€¢17mo ago
oh so what comand do i need to use now
Relevant
Relevantā€¢17mo 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
gododoā€¢17mo ago
ill try that it says i cant convert string to int hm
Relevant
Relevantā€¢17mo ago
1 sec, I'm playing around with it now
gododo
gododoā€¢17mo ago
ok
Relevant
Relevantā€¢17mo ago
yeah, tryparse would be better anyways
gododo
gododoā€¢17mo ago
where should i put this tryparse
Relevant
Relevantā€¢17mo 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
gododoā€¢17mo ago
should i write Byte instead of bytes where theres the error
Relevant
Relevantā€¢17mo ago
oops sorry, that was from my text code. I'll edit
gododo
gododoā€¢17mo ago
šŸ‘
Relevant
Relevantā€¢17mo ago
There ya go
gododo
gododoā€¢17mo ago
yooo it works thank you man šŸ™
Anchy
Anchyā€¢17mo 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
gododoā€¢17mo 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
gododoā€¢17mo ago
this was my windows form client
Anchy
Anchyā€¢17mo ago
the issue definitely looks like it lies in the client
Relevant
Relevantā€¢17mo ago
Yeah, wouldn't have been an issue if it was sent as an int
Anchy
Anchyā€¢17mo 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
gododoā€¢17mo ago
oh i see next time ill do that thank you for helping anyway
Anchy
Anchyā€¢17mo ago
do it how you like but I thought I would just weigh in on what the actual issue was šŸ˜„
gododo
gododoā€¢17mo ago
šŸ‘