加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

unsigned char *指向的内存区域大小是多少?

发布时间:2020-12-16 10:15:40 所属栏目:百科 来源:网络整理
导读:好的,我知道之前已经问过这个问题,但在搜索之后我找不到合适的答案. 我需要将缓冲区(unsigned char *)转换为base64,我使用的base64函数作为参数: void Base64Enc(const unsigned char *src,int srclen,unsigned char *dest) 其中int srclen是src字符串的长
好的,我知道之前已经问过这个问题,但在搜索之后我找不到合适的答案.

我需要将缓冲区(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: %sn",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,因此缓冲区中的元素数量只是缓冲区使用的空间量.

“按元素大小划分”是对任何类型元素的更通用的解决方案.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读