我真的对这件事感到困惑。令人难以置信的是,我发布了Chrome调试器的屏幕截图:

我在一个测试中快速编写了这个函数来比较两个base64编码的字符串(a可能比b短)。但是,它总是返回0,就好像第一个字符不同一样。事实上它们是:字符串a的第一个字符ca是正确的,但是由于某种神秘的原因,字符串b的第一个cb是空的!字符串b看起来是正确的,具有正确的长度(685个字符),并且具有正确的类型typeof(b) == 'string'
调用代码(实际上是TypeScript)是这样的,以防有帮助:
requestGET('qBandConfig.sql', { jobid: this.job.jobid }) // get the blob from db
.then(json => {
const base64:string = json[0]['bandconfig'] // type enforced to make sure
this.editor.setBands(base64)
.then(() => {
// test that we find the same blob when re-encoding
const check:string = this.editor.getBandsBLOB()
const diff = firstDiff(check, base64) // always return 0 ????
if (diff > -1 && check[diff] !== '=')
this.log.warning('encoded blob ', check, ' is different from decoded ', base64)
})
})发布于 2021-04-07 21:46:48
它可能包含一个不可打印的字符,比如在HTML语言中称为​的Zero Width Space。我不确定Chrome Dev工具在这种情况下会显示什么;它可能真的什么都不显示。
检查cb.length,如果它是0,那么cb是空的,否则它不是空的(很明显),然后你可以使用charCodeAt()来找出那里有什么。
使用零宽度空格字符的示例代码:
var x = "";
console.log("x === '" + x + "'");
console.log("x.length === " + x.length);
console.log("x.charCodeAt(0) === " + x.charCodeAt(0));
https://stackoverflow.com/questions/66987126
复制相似问题