-2

嗨,对于我的项目,我需要在 char 数组中的每个 x 字节添加空字节,例如

unsigned char data[] = {
0x98, 0xB0, 0x26, 0x7E, 0x11, 0x80, 0x9A, 0x79, 
0xE7, 0x46, 0x14, 0xA4, 0x62, 0x7E, 0x06, 0xC0 ...
};

需要:

unsigned char data[] = {
0x98, 0xB0, 0x26, 0x7E, 0x00, 0x11, 0x80, 0x9A, 0x79, 0x00 
0xE7, 0x46, 0x14, 0xA4, 0x00, 0x62, 0x7E, 0x06, 0xC0, 0x00...
};

我需要一个纯winapi中的函数没有std :: string我尝试了很多东西但我卡住了如果可以的话谢谢帮助我!:)

4

1 回答 1

0

此函数将在堆上分配的新数组上执行您想要的操作:

unsigned char* Process(const unsigned char* buffer, size_t size, size_t noChars, size_t& newSize)
{
   newSize = size + size/noChars;
   unsigned char* bufferProcessed = new unsigned char[newSize];

   size_t i;
   for (i = 0; i < size/noChars; ++i)
   {
       memcpy(bufferProcessed+(noChars+1)*i, buffer+noChars*i, noChars);
       bufferProcessed[(noChars+1)*(i+1)-1] = '\0';
   }
   if (size%noChars)
       memcpy(bufferProcessed+(noChars+1)*i, buffer+noChars*i, size-noChars*i);

   return bufferProcessed;
}

而这个则相反(注意变化!):

unsigned char* ProcessBack(const unsigned char* buffer, size_t size, size_t noChars, size_t& newSize)
{
    newSize = size - size/noChars;
    unsigned char* bufferProcessed = new unsigned char[newSize];

    size_t i;
    for (i = 0; i < size/noChars; ++i)
    {
        memcpy(bufferProcessed+noChars*i, buffer+(1+noChars)*i, noChars);
    }
    if (size%noChars)
        memcpy(bufferProcessed+noChars*i, buffer+(1+noChars)*i, size-noChars*i);

    return bufferProcessed;
}
于 2013-05-24T13:24:30.587 回答