【发布时间】:2018-07-05 09:28:59
【问题描述】:
所以我正在做一个加密/解密项目。
我想要做的是从行中读取 2 个字符,并将它们用作一个十六进制数来解密为原始十六进制值,并相应地打印到文件中正确的 ascii 字符值.. 这是我的代码:
sscanf(lineFromFile, "%2C", tmp);
//check carriage return new line as per outline
if(strcmp(tmp, "\n") == 0) {
fprintf(output, "%c", tmp);
}
//Check for tab whcih is set to TT in encryption scheme
if (strcmp(tmp, "TT") == 0) {
fprintf(output, "\t");
}
else {
outchar = ((tmp + i*2) + 16);
if (outchar > 127) {
outchar = (outchar - 144) + 32;
}
fprintf(output, "%C", outchar); //print directly to file
}
【问题讨论】:
-
回车是
'\r',换行是'\n'。"/n"只是斜线 & n -
签出
strtol。 -
什么是
i?((tmp + i*2) + 16)应该是什么? -
((tmp + i*2) + 16)等价于&tmp[i*2 + 16]。但由于tmp仅包含 2 个字符,因此您在初始化部分之外访问。 -
@Barmar 这是一个循环来检查字符直到它到达行尾。 ((tmp i*2) + 16) 尝试让它一次访问数组的两个元素以将 16 添加到