Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
函数输出错误:C中的Base 64解码_C_Encoding_Decoding - Fatal编程技术网

函数输出错误:C中的Base 64解码

函数输出错误:C中的Base 64解码,c,encoding,decoding,C,Encoding,Decoding,(我在网上找到了解码功能) 代码运行,但输出为false。 输出应该是:用户名:密码。但在本例中,输出是\006\busername:password。 我需要从decodedstring中提取用户名。因为用户名前面有额外的字符,所以无法使用 函数是否有问题,或者为什么我在开始时会得到这些额外的字符?解码器工作正常,问题是您没有初始化输出缓冲区。解码器始终使用strncat函数附加输出字符。您的输出缓冲区可能在statup处有一个垃圾值,因此实际的解码值将附加到垃圾值。在使用之前,添加一个mem

(我在网上找到了解码功能)

代码运行,但输出为false。 输出应该是:用户名:密码。但在本例中,输出是\006\busername:password。 我需要从decodedstring中提取用户名。因为用户名前面有额外的字符,所以无法使用


函数是否有问题,或者为什么我在开始时会得到这些额外的字符?

解码器工作正常,问题是您没有初始化输出缓冲区。解码器始终使用strncat函数附加输出字符。您的输出缓冲区可能在statup处有一个垃圾值,因此实际的解码值将附加到垃圾值。在使用之前,添加一个memset来初始化输出缓冲区,一切都会正常工作。正如Gil Hamilton在评论中提到的,该解码器仅适用于文本输出,尝试对二进制输出进行解码将导致错误

char decodedstring[30];
memset(&decodedstring[0], 0, 30);
ha = Base64Decode(authTable[0]->AuthenticationCred[k].userpassb64,decodedstring, outlen);
*userId = strtok(decodedstring, ":");

输入是什么?如果没有它,就无法判断您的输出是否正确。顺便说一下,您发现的代码有错误:如果解码的输出中有空值怎么办?strncat不会复制超出此范围的内容。还有其他问题。找到一个更好的实现:还有其他几个可用的实现。
char decodedstring[10];
ha = Base64Decode(authTable[0]->AuthenticationCred[k].userpassb64,decodedstring, outlen);
*userId = strtok(decodedstring, ":");
char decodedstring[30];
memset(&decodedstring[0], 0, 30);
ha = Base64Decode(authTable[0]->AuthenticationCred[k].userpassb64,decodedstring, outlen);
*userId = strtok(decodedstring, ":");