Search code examples
c#c++qtqbytearray

Convert int to little-endian formated bytes in C++ for blobId in Azure


Working with a base64 encoding for Azure (http://msdn.microsoft.com/en-us/library/dd135726.aspx) and I dont seem to work out how to get the required string back. I'm able to do this in C# where I do the following.

int blockId = 5000;
var blockIdBytes = BitConverter.GetBytes(blockId);
Console.WriteLine(blockIdBytes);
string blockIdBase64 = Convert.ToBase64String(blockIdBytes);
Console.WriteLine(blockIdBase64);

Which prints out (in LINQPad):

Byte[] (4 items)
| 136         |
| 19          |
| 0           |
| 0           |

iBMAAA==

In Qt/C++ I tried a few aporaches, all of them returning the wrong value.

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=="

How can I get the same result as the C# version?


Solution

  • I ended up doing the following:

    QByteArray temp;
    int blockId = 5000;
    
    for(int i = 0; i != sizeof(blockId); i++) {
      temp.append((char)(blockId >> (i * 8)));
    }
    
    qDebug() << temp.toBase64(); // "iBMAAA==" which is correct