C语言字符串去除后面的无用字节(可用于进程间通信)

C语言字符串去除后面的无用字节(可用于进程间通信)

#include 
#include 


#define dPrint(fmt, ...) do{fprintf(stderr, "[%s:%d] " fmt "\r\n", __FUNCTION__, __LINE__, ##__VA_ARGS__);}while(0)
#define HexPrint(_buf, _len) \
{\
    int _m_i = 0;\
    char *_m_buf = (char *)(_buf);\
    int _m_len = (int)(_len);\
    printf("[%s:%d] \r\n", __FUNCTION__, __LINE__);\
    printf("*****************************\n");\
    for(_m_i = 0; _m_i < _m_len; _m_i++)\
    {\
        printf("\033[32m%02x \033[0m", _m_buf[_m_i] & 0xff);\
        if(!((_m_i+1) % 10)) printf("\n");\
    }\
    printf("\nsize = %d\n*****************************\n", _m_len);\
}


unsigned int CompressData(const char *pcData, unsigned int uiLen)
{
    unsigned int uiIdx = 0;
    unsigned int uiInvalid = 0;
    
    for(uiIdx = 0; uiIdx < uiLen; uiIdx++)
    {
        if(0 == pcData[uiIdx]) uiInvalid++;
        else uiInvalid = 0;
    }


    return (uiLen - uiInvalid);
}




int main()
{
    unsigned char ucData[1024];


    memset(ucData, 0, sizeof(ucData));
    ucData[0] = 5;
    ucData[566] = 1;


    dPrint("Valid Data : %d", CompressData((const char *)ucData, sizeof(ucData)));
    


    return 0;
}

你可能感兴趣的:(C/C++编程,字符串,c语言,信息压缩)