Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Process a String in Increments?

Status
Not open for further replies.

Stripey

MIS
Sep 19, 2005
3
ES
I have an MFC application that allows a user to input a string of up to 400 charachters. I need to then allocate memory in a buffer to do some processing on the data (encryption). How can I ensure that the size of the buffer is divisible by 8 and still allocate the memory dynamically?

Thanks for any help.

Stripey
 
You need to allocate ((strlen(buffer) / 8) + (strlen(buffer) % 8) ? 1 : 0) * 8 bytes - and pad the last (allocated length) - strlen(buffer) % 8 - 1 bytes with whatever dummy data you need.
 
So, something like this (assuming szMessage has data in it)? How would you go about padding the data? Thanks for the reply.

Stripey

Code:
LPTSTR szMessage;
PVOID pvBuffer;
int nSize;

nSize = ((strlen(szMessage) / 8) + (strlen(szMessage) % 8) ? 1 : 0) * 8;

pvBuffer = VirtualAlloc(NULL, nAmount,  MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top