sludgy character array problem
-
I'm having problems with an array tacking a bunch of sludge on to the end of a array of random characters. It needs to generate an array of 64 random bytes for use inan encryption routine. I'm getting the 64 bytes of information that I want at the front end of the array, BUT..it's also tacking on the contents of the original array that I filled with a sequence from ASCII 32 to ASCII 127 character values. My question is how to get the sludge off the end of the array so it consists of just teh 64 random byte values. Source Code Follows:
// seed a random number generator with the current
// tick count. we will use this as an iterator in
// the szRandomBytes array a bit latersrand(GetTickCount());
// set some values so that we only get legal characters
// from the szStringArray which will be used to fill
// the szRandomBytes array with legal charactersint nElementCount = 0;
int nElementValue = 32;// set up our two arrays. szStringArray gets filled
// with legal character values. szRandomBytes uses
// the values in szStringArray to generate a random
// character string that we can use for encryptionchar szStringArray[95] = "";
char szRandomBytes[64] = "";// filling the array
while ( nElementCount <= 94 )
{
szStringArray[nElementCount] = nElementValue;
nElementCount++;
nElementValue++;
}// set iterator to zero
nElementCount = 0;
// pull 64 random characters out of the array
// to create our random string for encodingwhile (nElementCount <=63)
{// get a random number int nRandomNumber = rand(); // checking it out. if its greater than 95 // we divide it until its with the bounds of // the szStringArray. while (nRandomNumber >= 95) { nRandomNumber = nRandomNumber / 2; } // using nRandomNumber and szStringArray to assign // a value to the Element Number that szRandomBytes // is currently on up to a possible 64 characters szRandomBytes\[nElementCount\] = szStringArray\[nRandomNumber\]; // incrementing the element count so // that the next value gets filled. nElementCount++;
}
// debug check
MessageBox(NULL, szRandomBytes, "Random String From Array", MB_OK);
It's good to see kids turning their minds to wholesum activities such as programming, instead of wasting their lives in the hedonistic disciplines of Sex, Drugs, & Rock & Roll... or Sex with Drugs, or Sex with Rocks while Rolling in Drugs, or whatever new-fangled perversions you
-
I'm having problems with an array tacking a bunch of sludge on to the end of a array of random characters. It needs to generate an array of 64 random bytes for use inan encryption routine. I'm getting the 64 bytes of information that I want at the front end of the array, BUT..it's also tacking on the contents of the original array that I filled with a sequence from ASCII 32 to ASCII 127 character values. My question is how to get the sludge off the end of the array so it consists of just teh 64 random byte values. Source Code Follows:
// seed a random number generator with the current
// tick count. we will use this as an iterator in
// the szRandomBytes array a bit latersrand(GetTickCount());
// set some values so that we only get legal characters
// from the szStringArray which will be used to fill
// the szRandomBytes array with legal charactersint nElementCount = 0;
int nElementValue = 32;// set up our two arrays. szStringArray gets filled
// with legal character values. szRandomBytes uses
// the values in szStringArray to generate a random
// character string that we can use for encryptionchar szStringArray[95] = "";
char szRandomBytes[64] = "";// filling the array
while ( nElementCount <= 94 )
{
szStringArray[nElementCount] = nElementValue;
nElementCount++;
nElementValue++;
}// set iterator to zero
nElementCount = 0;
// pull 64 random characters out of the array
// to create our random string for encodingwhile (nElementCount <=63)
{// get a random number int nRandomNumber = rand(); // checking it out. if its greater than 95 // we divide it until its with the bounds of // the szStringArray. while (nRandomNumber >= 95) { nRandomNumber = nRandomNumber / 2; } // using nRandomNumber and szStringArray to assign // a value to the Element Number that szRandomBytes // is currently on up to a possible 64 characters szRandomBytes\[nElementCount\] = szStringArray\[nRandomNumber\]; // incrementing the element count so // that the next value gets filled. nElementCount++;
}
// debug check
MessageBox(NULL, szRandomBytes, "Random String From Array", MB_OK);
It's good to see kids turning their minds to wholesum activities such as programming, instead of wasting their lives in the hedonistic disciplines of Sex, Drugs, & Rock & Roll... or Sex with Drugs, or Sex with Rocks while Rolling in Drugs, or whatever new-fangled perversions you
Put a terminating character at teh end of your string. The last character in your array should be '\0'. You are trying to display a string in your MessageBox by putting the array of char but are not giving your array of char a termination indicating this is where the string ends.
-
I'm having problems with an array tacking a bunch of sludge on to the end of a array of random characters. It needs to generate an array of 64 random bytes for use inan encryption routine. I'm getting the 64 bytes of information that I want at the front end of the array, BUT..it's also tacking on the contents of the original array that I filled with a sequence from ASCII 32 to ASCII 127 character values. My question is how to get the sludge off the end of the array so it consists of just teh 64 random byte values. Source Code Follows:
// seed a random number generator with the current
// tick count. we will use this as an iterator in
// the szRandomBytes array a bit latersrand(GetTickCount());
// set some values so that we only get legal characters
// from the szStringArray which will be used to fill
// the szRandomBytes array with legal charactersint nElementCount = 0;
int nElementValue = 32;// set up our two arrays. szStringArray gets filled
// with legal character values. szRandomBytes uses
// the values in szStringArray to generate a random
// character string that we can use for encryptionchar szStringArray[95] = "";
char szRandomBytes[64] = "";// filling the array
while ( nElementCount <= 94 )
{
szStringArray[nElementCount] = nElementValue;
nElementCount++;
nElementValue++;
}// set iterator to zero
nElementCount = 0;
// pull 64 random characters out of the array
// to create our random string for encodingwhile (nElementCount <=63)
{// get a random number int nRandomNumber = rand(); // checking it out. if its greater than 95 // we divide it until its with the bounds of // the szStringArray. while (nRandomNumber >= 95) { nRandomNumber = nRandomNumber / 2; } // using nRandomNumber and szStringArray to assign // a value to the Element Number that szRandomBytes // is currently on up to a possible 64 characters szRandomBytes\[nElementCount\] = szStringArray\[nRandomNumber\]; // incrementing the element count so // that the next value gets filled. nElementCount++;
}
// debug check
MessageBox(NULL, szRandomBytes, "Random String From Array", MB_OK);
It's good to see kids turning their minds to wholesum activities such as programming, instead of wasting their lives in the hedonistic disciplines of Sex, Drugs, & Rock & Roll... or Sex with Drugs, or Sex with Rocks while Rolling in Drugs, or whatever new-fangled perversions you