2

你能告诉我如何通过 Qt lib 将 C# 中的这些函数重写为 C++ 吗?

private MemoryStream _memoryStream= new MemoryStream();

//WriteUTFBytes

public void WriteUTFBytes(string value)
{
    //Length - max 65536.
    UTF8Encoding utf8Encoding = new UTF8Encoding();
    byte[] buffer = utf8Encoding.GetBytes(value);
    if (buffer.Length > 0)
    WriteBytes(buffer);
}

//WriteBytes

public void WriteBytes(byte[] buffer)
{

        for (int i = 0; buffer != null && i < buffer.Length; i++)
            _memoryStream.WriteByte(buffer[i]);
}

//WriteByte

public void WriteByte(int value)
    {
        _memoryStream.WriteByte((byte)value);
    }

//WriteShort

public void WriteShort(int value)
    {
        byte[] bytes = BitConverter.GetBytes((ushort)value);
        WriteBigEndian(bytes);
    }

//WriteBigEndian

private void WriteBigEndian(byte[] bytes)
    {
        if (bytes == null)
            return;
        for (int i = bytes.Length - 1; i >= 0; i--)
        {
            _memoryStream.WriteByte(bytes[i]);
        }
    }

4

1 回答 1

3
private:
    QBuffer _buffer;

public:
    YourClass()
    {
        _buffer.open(QIODevice::WriteOnly);
    }

    void writeUtfBytes(const QString &value)
    {
        QTextStream ts(&_buffer);
        ts.setCodec("UTF-8");
        ts << value;
    }

    void writeBytes(const char *data, int size)
    {
        if (data)
            _buffer.write(data, size);
    }

    void writeByte(int value)
    {
        _buffer.putChar(value);
    }

    void writeShort(int value)
    {
        _buffer.putChar(value >> 8);
        _buffer.putChar((char) value);
    }
于 2013-08-19T14:52:51.500 回答