Today I am trying to copy a unsigned long
variable into the contents of an unsigned char *
variable.
The reasoning for this is, I wrote an RC4 cipher which requires the key input to be a unsigned char *
, I am using the SYSTEMTIME
class to obtain a value & combining it with a randomly generated long
value to obtain my key for RC4 - I am using it as a timestamp for a user created account to mark in my sqlite dbs.
Anyways, the problem I ran into is that I cannot copy the ULONG
into PUCHAR
.
I've tried
wsprintfA(reinterpret_cast<LPSTR>(ucVar), "%lu", ulVar);
and I've tried
wsprintfA((LPSTR)ucVar, "%lu", ulVar);
However, after executing my program the result in ucVar
is just empty, or it doesn't even compute, and crashing the application.
[edit 1]
I thought maybe the memcpy
approach would work, so I tried declaring another variable and moving it into ucVar
, but it still crashed the application - i.e. It didn't reach the MessageBox()
:
unsigned char *ucVar;
char tmp[64]; // since ulVar will never be bigger than 63 character + 1 for '\0'
wsprintfA(tmp, "%lu", ulVar);
memcpy(ucVar, tmp, sizeof(tmp));
MessageBox(0, (LPSTR)ucVar, "ucVar", 0);
[/edit 1]
[edit 2]
HeapAlloc() on ucVar with of size 64 fixed my problem, thank you ehnz for your suggestion!
[/edit 2]
Can anyone give me some approach to this problem? It is greatly appreciated!
Regards, Andrew