943,649 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2483
  • C++ RSS
Jul 4th, 2008
0

quick question: how to 'null' blocks of an unsigned char array

Expand Post »
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!!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
adamj2 is offline Offline
16 posts
since Jun 2008
Jul 4th, 2008
0

Re: quick question: how to 'null' blocks of an unsigned char array

>>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.
Last edited by Ancient Dragon; Jul 4th, 2008 at 10:48 am.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is online now Online
21,948 posts
since Aug 2005
Jul 4th, 2008
0

Re: quick question: how to 'null' blocks of an unsigned char array

>>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.
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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
adamj2 is offline Offline
16 posts
since Jun 2008
Jul 4th, 2008
0

Re: quick question: how to 'null' blocks of an unsigned char array

Hi

possibly you want that second array of chars behaves like first on, as in:
C++ Syntax (Toggle Plain Text)
  1. int main() {
  2. unsigned char apayload[] = {'H','i',' ','T','h','e','r','e', '\0'};
  3. unsigned char received_payload[]={'H','i',' ','T','h','e','r','e','a', 'b','\0'};
  4. cout << apayload << " "<< received_payload << endl;
  5. received_payload[8]='\0';
  6. cout << apayload << " " << received_payload << endl;
  7. return 0;
  8. }
  9. /* result
  10. Hi There Hi Thereab
  11. Hi There Hi There
  12. */
You may consider what I added to the initialization of both arrays. Also consider this solution will produce memory leaks.

krs,
tesu
Reputation Points: 158
Solved Threads: 98
Master Poster
tesuji is offline Offline
720 posts
since Apr 2008
Jul 4th, 2008
0

Re: quick question: how to 'null' blocks of an unsigned char array

Click to Expand / Collapse  Quote originally posted by adamj2 ...
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?
I guess you are talking about std::string c++ class and not character arrays.
C++ Syntax (Toggle Plain Text)
  1. std::string = "Hello World";
  2. // now cut off the last two characters of the string
  3. string = string.substr(0, string.length()-2);
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is online now Online
21,948 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Writing Derived Objects to files
Next Thread in C++ Forum Timeline: damages when executing console from MFC





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC