【发布时间】:2012-04-29 11:50:43
【问题描述】:
使用 Azure (http://msdn.microsoft.com/en-us/library/dd135726.aspx) 的 base64 编码,我似乎不知道如何取回所需的字符串。我可以在 C# 中执行以下操作。
int blockId = 5000;
var blockIdBytes = BitConverter.GetBytes(blockId);
Console.WriteLine(blockIdBytes);
string blockIdBase64 = Convert.ToBase64String(blockIdBytes);
Console.WriteLine(blockIdBase64);
打印出来(在 LINQPad 中):
Byte[] (4 items)
| 136 |
| 19 |
| 0 |
| 0 |
iBMAAA==
在 Qt/C++ 中,我尝试了一些 aporache,它们都返回了错误的值。
const int a = 5000;
QByteArray b;
for(int i = 0; i != sizeof(a); ++i) {
b.append((char)(a&(0xFF << i) >>i));
}
qDebug() << b.toBase64(); // "iIiIiA=="
qDebug() << QByteArray::number(a).toBase64(); // "NTAwMA=="
qDebug() << QString::number(a).toUtf8().toBase64(); // "NTAwMA=="
如何获得与 C# 版本相同的结果?
【问题讨论】:
-
想想当
i为 1 时你的循环做了什么。当你想要 0xFF00 时,它将 0xFF 左移一位以获得 0x1FE。 -
有没有更简单的方法可以在不使用 for 循环的情况下获得相同的结果?
标签: c# c++ qt qbytearray