| | |
quick question: how to 'null' blocks of an unsigned char array
![]() |
•
•
Join Date: Jun 2008
Posts: 16
Reputation:
Solved Threads: 0
Hi there,
Say I have a payload:
1) unsigned char apayload[] = {'H','i',' ','T','h','e','r','e'};
And then I have another payload, which is the same as above but with an additional 2 characters:
2) unsigned char received_payload[]={'H','i',' ','T','h','e','r','e,'a','b''};
How would I go about making the second payload exactly the same as the first payload?
I basically want to null out array blocks [8] and [9] of the second payload so that the characters 'a' and 'b' are ignored basically no longer exist so it becomes a payload of size [8] like the first one.
I am trying this at the moment but it does not work as I guess it is just adding "0" as a character to the array and not terminating that block all together.
for (int i=8;i <10; i++)
received_payload[i] = 0;
Thankyou!!
Say I have a payload:
1) unsigned char apayload[] = {'H','i',' ','T','h','e','r','e'};
And then I have another payload, which is the same as above but with an additional 2 characters:
2) unsigned char received_payload[]={'H','i',' ','T','h','e','r','e,'a','b''};
How would I go about making the second payload exactly the same as the first payload?
I basically want to null out array blocks [8] and [9] of the second payload so that the characters 'a' and 'b' are ignored basically no longer exist so it becomes a payload of size [8] like the first one.
I am trying this at the moment but it does not work as I guess it is just adding "0" as a character to the array and not terminating that block all together.
for (int i=8;i <10; i++)
received_payload[i] = 0;
Thankyou!!
>>How would I go about making the second payload exactly the same as the first payload?
You can't without reallocating the entire string because the way the arrays are declared there is no room for expansion.
BTW: neither apayload nor received_payload are null-terminated strings. When you initialize character arrays like that using individual characters the array is nothing more than an array of characters, so you can't use string functions on them such as strlen() to get the number of characters.
>>I am trying this at the moment but it does not work as I guess it is just adding "0" as a character to the array and not terminating that block all together.
Yes, that only replaces the character with 0, it does not change the array size. You will have to reallocate the array if you want to make it smaller. But I don't think its worth the effort. Just put a 0 there and treat it like any other null-terminated string.
You can't without reallocating the entire string because the way the arrays are declared there is no room for expansion.
BTW: neither apayload nor received_payload are null-terminated strings. When you initialize character arrays like that using individual characters the array is nothing more than an array of characters, so you can't use string functions on them such as strlen() to get the number of characters.
>>I am trying this at the moment but it does not work as I guess it is just adding "0" as a character to the array and not terminating that block all together.
Yes, that only replaces the character with 0, it does not change the array size. You will have to reallocate the array if you want to make it smaller. But I don't think its worth the effort. Just put a 0 there and treat it like any other null-terminated string.
Last edited by Ancient Dragon; Jul 4th, 2008 at 10:48 am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
Join Date: Jun 2008
Posts: 16
Reputation:
Solved Threads: 0
•
•
•
•
>>How would I go about making the second payload exactly the same as the first payload?
You can't without reallocating the entire string because the way the arrays are declared there is no room for expansion.
BTW: neither apayload nor received_payload are null-terminated strings. When you initialize character arrays like that using individual characters the array is nothing more than an array of characters, so you can't use string functions on them such as strlen() to get the number of characters.
>>I am trying this at the moment but it does not work as I guess it is just adding "0" as a character to the array and not terminating that block all together.
Yes, that only replaces the character with 0, it does not change the array size. You will have to reallocate the array if you want to make it smaller. But I don't think its worth the effort. Just put a 0 there and treat it like any other null-terminated string.
If I wanted to do the same sort of thing with strings but then convert it using c_str() is there a way to add to the c_str() function to do this or would I have to re-allocate etc first before using c_str()?
Ie if I wanted to convert the first 8 characters of the string using c_str() but ignore the last 2 characters, can I instruct it to do this with c_str() or would I have to change the string to get rid of characters 9 and 10 before hand?
•
•
Join Date: Apr 2008
Posts: 296
Reputation:
Solved Threads: 42
Hi
possibly you want that second array of chars behaves like first on, as in:
You may consider what I added to the initialization of both arrays. Also consider this solution will produce memory leaks.
krs,
tesu
possibly you want that second array of chars behaves like first on, as in:
C++ Syntax (Toggle Plain Text)
int main() { unsigned char apayload[] = {'H','i',' ','T','h','e','r','e', '\0'}; unsigned char received_payload[]={'H','i',' ','T','h','e','r','e','a', 'b','\0'}; cout << apayload << " "<< received_payload << endl; received_payload[8]='\0'; cout << apayload << " " << received_payload << endl; return 0; } /* result Hi There Hi Thereab Hi There Hi There */
krs,
tesu
Information is moving—you know, nightly news is one way, of course, but it's also moving through the blogosphere and through the Internets. I promise you I will listen to what has been said here, even though I wasn't here. Ann and I will carry out this equivocal message to the world. I'm the master of low expectations.
•
•
•
•
OK, cool, thanks for that.
If I wanted to do the same sort of thing with strings but then convert it using c_str() is there a way to add to the c_str() function to do this or would I have to re-allocate etc first before using c_str()?
Ie if I wanted to convert the first 8 characters of the string using c_str() but ignore the last 2 characters, can I instruct it to do this with c_str() or would I have to change the string to get rid of characters 9 and 10 before hand?
C++ Syntax (Toggle Plain Text)
std::string = "Hello World"; // now cut off the last two characters of the string string = string.substr(0, string.length()-2);
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
![]() |
Other Threads in the C++ Forum
- Previous Thread: Writing Derived Objects to files
- Next Thread: damages when executing console from MFC
| Thread Tools | Search this Thread |
api array auto based beginner binary bitmap c++ c/c++ calculator challenge char class classes code coding compile console conversion count delay-loading delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption equation error file forms fstream function functions game garbage givemetehcodez graph gui hmenu homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker loop looping loops map math matrix memory multiple news node output parameter performance pointer primenumbersinrange problem program programming project python random read recursion reference rpg sockets string strings temperature template test text text-file tivoli tree url variable vector video win32 windows winsock wordfrequency wxwidgets






