比较 MATLAB 问题中的字符串

发布于 2024-11-27 21:09:13 字数 1232 浏览 0 评论 0 原文

我一直在摆弄我的程序,并且一直在使用允许 BASIC 身份验证的 urlread 的修改版本。问题是我必须将以下代码行包含到基本 urlread 函数中:

urlConnection.setRequestProperty('Authorization', 'Basic passphrase');

...其中密码是“user:pass”的 base64 编码字符串。如果我将密码直接放入该行的字符串中,程序将正常工作,但当我尝试连接以获得结果“基本密码”字符串时,麻烦就开始了。最初我只是:

['Basic', ' ', passphrase]

之后不起作用,我在命令窗口中进行了一些探索和实验。:

passphrase = 'somerandompassphrase';
teststr1 = ['Basic', ' ', passphrase];
teststr2 = ['Basic', ' ', 'somerandompassphrase'];
teststr3 = 'Basic somerandompassphrase';
strcmp(teststr1, teststr2)
strcmp(teststr1, teststr3)
strcmp(teststr2, teststr3)

输出为 1,或者每个都为 true(如预期)。但是,如果我采用“somerandompassphrase”的base64encode(即“c29tZXJhbmRvbXBhc3NwaHJhc2U=”):

encoded = base64encode(passphrase);
teststr1 = ['Basic', ' ', encoded];
teststr2 = ['Basic', ' ', 'c29tZXJhbmRvbXBhc3NwaHJhc2U='];
strcmp(teststr1, teststr2)

输出为0或错误。但这不应该是真的吗? base64encode 函数可以在此处找到。

即使经过快速测试:

strcmp(encoded, 'c29tZXJhbmRvbXBhc3NwaHJhc2U=')

输出仍然是 0。

请帮忙,我不知道发生了什么。

I've been fiddling around with my program and I've been using a modified version of urlread that allows for BASIC authentication. The problem is that I have to include the following line of code to the base urlread function:

urlConnection.setRequestProperty('Authorization', 'Basic passphrase');

...where passphrase is the a base64 encoded string of 'user:pass'. If I place the passphrase directly into the string on that line the program will work just fine, the trouble starts when I try to concatenate to get that resulting 'Basic passphrase' string. Initially I just had:

['Basic', ' ', passphrase]

After that did not work I did some exploring and experimenting around in the command window.:

passphrase = 'somerandompassphrase';
teststr1 = ['Basic', ' ', passphrase];
teststr2 = ['Basic', ' ', 'somerandompassphrase'];
teststr3 = 'Basic somerandompassphrase';
strcmp(teststr1, teststr2)
strcmp(teststr1, teststr3)
strcmp(teststr2, teststr3)

The output is 1, or true for each one (as expected). However if I take the base64encode of 'somerandompassphrase' (which is 'c29tZXJhbmRvbXBhc3NwaHJhc2U='):

encoded = base64encode(passphrase);
teststr1 = ['Basic', ' ', encoded];
teststr2 = ['Basic', ' ', 'c29tZXJhbmRvbXBhc3NwaHJhc2U='];
strcmp(teststr1, teststr2)

The output is 0, or false. Shouldn't it be true though? The base64encode function can be found here.

Even from a quick test of:

strcmp(encoded, 'c29tZXJhbmRvbXBhc3NwaHJhc2U=')

The output is still 0.

Please help, I have no idea what's going on.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

苦笑流年记忆 2024-12-04 21:09:13

此处所示,您还可以使用来自 base64 编码器编解码器/” rel="nofollow">Apache Commons Codec Java 库与 MATLAB 捆绑在一起,可在类路径中使用:

encoder = org.apache.commons.codec.binary.Base64();
b64str = char( encoder.encode(passphrase-0) )';

As shown here, you can also use the base64 encoder from the the Apache Commons Codec Java library which comes bundled with MATLAB and is available on the classpath:

encoder = org.apache.commons.codec.binary.Base64();
b64str = char( encoder.encode(passphrase-0) )';
蒲公英的约定 2024-12-04 21:09:13

实际上,我在发布问题之前就已经弄清楚了这一点,但我想我会继续并保留它,以防人们遇到与我相同的问题。

问题出在 base64encode 函数上。它会自动在字符串末尾添加换行符,导致 strcmp 函数返回 false。要解决此问题,您可以在 base64encode 函数中包含一个可选参数的参数,如果您放入空白字符串,则不会在其末尾添加换行符,从而使其正常工作。

encoded = base64encode(passphrase, '');

I actually figured this out right before I posted the question, but I figured I'd go ahead and leave it up in case people run into the same problem as I did.

The problem is from the base64encode function. It automatically adds a newline character to the end of the string, causing the strcmp function to return false. To fix this you can include a parameter for the optional parameter to the base64encode function, if you put in a blank string it won't add a newline character to the end of it causing it to work.

encoded = base64encode(passphrase, '');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文