如何检查 C 中十六进制编码字符串中的“00”?

How to check for "00" in a hex-coded string in C?

我正在尝试采用十六进制编码的字符串值并将其解码为普通文本。字符串中的值应该用“00”分隔,表示 NULL,所以我试图检查“00”何时出现,并将解码字符串中的值替换为“,”,以便稍后分隔值.


int main()
{
    char decoded[256];
    const char hex[] = "746573743d7468697300746573743d74686973";
    strcpy(decoded, "");
    char curr[4];
    char next[2];
    int i = 0;
    while (i+1 < strlen(hex)){
        if((hex[i] == 0) && (hex[i+1] == 0)){
            sprintf(decoded + strlen(decoded), ",");
            i = i+2;
        }
        else{
            strcpy(curr, "");
            strcpy(next, "");
            sprintf(curr, "%c", hex[i]);
            sprintf(next, "%c", hex[i+1]);
            strcat(curr, next);
            int num = (int)strtol(curr, NULL, 16);
            sprintf(decoded + strlen(decoded), "%c", num);
            i = i+2;
        }
    }
    printf("%s", decoded);
    return 0;
}

当 运行 我得到:

test=thistest=this

我想要的是:

测试=这个,测试=这个

我试过通过检查是否 hex[i] = 0 并打印一些东西来进行调试,但也没有任何结果。有任何想法吗?谢谢!

if((hex[i] == 0) && (hex[i+1] == 0)){

您的条件正在检查 0。您要检查 字符 '0':

if((hex[i] == '0') && (hex[i+1] == '0')){