Ok, I know this has been asked before but after searching I couldn’t find a proper answer.
I need to convert a buffer (unsigned char *) to base64, the base64 function I am using takes as paramters:
void Base64Enc(const unsigned char *src, int srclen, unsigned char *dest)
where int srclen is the length of the src string.
My question is, how do I get the length of my buffer. No, it’s not null terminated. No, I don’t want the sizeof(BYTE). I just need to know what to pass to as srclen so i can convert that buffer to base64.
EDIT:
Here’s some code showing what I am doing:
unsigned char *pBytes;
unsigned char *B64Encoded;
int b64size = 0;
if (pBytes = (unsigned char *) GlobalLock(hMem))
{
DWORD size = (DWORD)GlobalSize(hMem);
b64size = size / sizeof(unsigned char);
Base64Enc(pBytes, b64size, B64Encoded);
// in this case save the buffer to a file just for testing
if (fp = fopen("ububub.txt", "wb"))
{
printf("data: %s\n", B64Encoded);
fwrite(B64Encoded, strlen(B64Encoded), 1, fp);
fclose(fp);
}
}
If it’s not NULL terminated or something-terminated, the only way to know the size is to keep track of it when you first got it.
How are you getting the buffer in the first place? Whatever that is probably gives you a method of also obtaining the length of the buffer.
EDIT
Your comment:
In that case, take your raw size and divide it by the size of each element to get the number of elements:
Thanks to Todd Gardner for pointing out that sizeof(unsigned char) is defined by the standard to be 1, so the number of elements in your buffer is just the amount of space your buffer uses.
The "divide by size of element" is the more general solution to any type of element.