好的,我知道之前有人问过这个问题,但在搜索之后我找不到合适的答案。
我需要将缓冲区 (unsigned char *) 转换为 base64,我正在使用的 base64 函数作为参数:
void Base64Enc(const unsigned char *src, int srclen, unsigned char *dest)
其中 int srclen
是 src
字符串的长度。
我的问题是,如何获得缓冲区的长度。不,它不是空终止的。不,我不想要 sizeof(BYTE)
。我只需要知道要传递给 srclen
的内容,这样我就可以将该缓冲区转换为 base64。
编辑:
这里有一些代码展示了我在做什么:
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);
}
}
最佳答案
如果它不是 NULL 终止或某些东西终止,了解大小的唯一方法是在您第一次获得它时跟踪它。
您首先是如何获得缓冲区的?无论那是什么,都可能为您提供一种获取缓冲区长度的方法。
编辑
您的评论:
I have the size of the memory that the data takes. I tried playing with that but it's not the correct information apparently.
在这种情况下,将您的原始大小除以每个元素的大小以获得元素的数量:
unsigned numelem = size / sizeof(unsigned char)
感谢Todd Gardner指出 sizeof(unsigned char) 由标准定义为 1,因此缓冲区中的元素数就是缓冲区使用的空间量。
“按元素大小划分”是针对任何类型元素的更通用的解决方案。
关于c - 一个unsigned char *指向的内存区大小是多少?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1035222/