working with byte arrays
-
So I still need to terminate the string with \0 Having trouble with the length, I did quick cheat with
int msgLength = bytesReceived - 3;
//int msgLength = recvbuf[2] << 8 + recvbuf[1];
char* szWords = new char[msgLength + 1];
strncpy(szWords, recvbuf + 3, msgLength);int msgLength with is an integer, I keep getting 0, is it because recvbuf[2] is returning the string, but wait, it's a byte representing a number. hmm
Sorry, I really need to test my code before posting here, and check my operator precedence rules. In my defence, it is getting late and I'm half way through a nice bottle of Merlot :-D Try:
int msgLength = (recvbuf[2] << 8) + recvbuf[1];
Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman
-
Sorry, I really need to test my code before posting here, and check my operator precedence rules. In my defence, it is getting late and I'm half way through a nice bottle of Merlot :-D Try:
int msgLength = (recvbuf[2] << 8) + recvbuf[1];
Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman
Hey no problem, I thought perhaps I had to solve it as a test of some sort. I answer asp.net server control questions on my last beer before I turn off the computer at night. I want to thank you for helping me. It was really bring me down, and everyone just sort of gave me a blank look, "Its so easy, come on, that c++ 101". Words cannot express the gratitude for the help. I really appreciate it, and yes, code project pulls through with expert help.
-
Hey no problem, I thought perhaps I had to solve it as a test of some sort. I answer asp.net server control questions on my last beer before I turn off the computer at night. I want to thank you for helping me. It was really bring me down, and everyone just sort of gave me a blank look, "Its so easy, come on, that c++ 101". Words cannot express the gratitude for the help. I really appreciate it, and yes, code project pulls through with expert help.
:laugh: great fun but we didnt solve the passing of byte arrays
-
:laugh: great fun but we didnt solve the passing of byte arrays
oh no, it got solved! Richard wrote some code to extract the message size from byte 2, copy just the message to a new char array, termintate it with \0, and pass it to my function for processing I'm stoked, surfer happiness, yeah! I've been banging my head for days on this, feeling pretty stupid about it. Thanks for watching the show! msgLength = (recvbuf[2] << 8) + recvbuf[1]; szWords = new char[msgLength + 1]; strncpy(szWords, recvbuf + 3, msgLength); szWords[msgLength] = '\0'; _process_SQL_BufferData(szWords, bytesReceived);
-
Hey no problem, I thought perhaps I had to solve it as a test of some sort. I answer asp.net server control questions on my last beer before I turn off the computer at night. I want to thank you for helping me. It was really bring me down, and everyone just sort of gave me a blank look, "Its so easy, come on, that c++ 101". Words cannot express the gratitude for the help. I really appreciate it, and yes, code project pulls through with expert help.
Happy to help, it's what makes CodeProject fun! That and your appreciation.
Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman
-
I understand now that the data I receive from a socket is a byte array. But I don't understand how to pass the byte array to a function for further processing. Plus I'm not sure if I should leave it as a byte array, or try to convert it to a string, so I can extract data from it. I'm confused as to whether this buffer is a pointer to data somewhere else, and If I need to make a copy of it, before passing it to a function. When I pass it to a function, the data is lost. Kind of tired of going around in circles here on this, and I need road map to head in the right direction. I make my receive buffer char recvbuf[256]; I get data 0x05 '' 0x54 'T' 0x53 'S' 0x65 'e' And I try to pass it to a function for processing. Unless just extracting the info I need does not require a function. Suggestion? void _process(char *pbuffer) { }
-
jkirkerx wrote:
When I pass it to a function, the data is lost.
What does that mean exactly? One possibility is that you are misusing the buffer. If so that has nothing to do with sockets nor with how you chose to process the data.
Only the first char in the byte array passed to the function. I suspect that the 2nd byte was the data size of the packet, and the 3 byte was used for some control purpose, or an extension of the 2nd byte or something. So when I go the function I passed the data to, just the first byte is there 'T' or something. I think it was a 0x05. I have it working now, so far so good, and it's not crashing yet. I ended up passing the data in a vector to the function.
-
Only the first char in the byte array passed to the function. I suspect that the 2nd byte was the data size of the packet, and the 3 byte was used for some control purpose, or an extension of the 2nd byte or something. So when I go the function I passed the data to, just the first byte is there 'T' or something. I think it was a 0x05. I have it working now, so far so good, and it's not crashing yet. I ended up passing the data in a vector to the function.
Doesn't matter what the other data was. What matters is that it was missing. Some possible reasons - You copied from one buffer to another incorrectly. - You overwrote the original buffer - You didn't have all the data in the buffer in the first place. - You are indexing incorrectly in the processing stream.
-
Doesn't matter what the other data was. What matters is that it was missing. Some possible reasons - You copied from one buffer to another incorrectly. - You overwrote the original buffer - You didn't have all the data in the buffer in the first place. - You are indexing incorrectly in the processing stream.