2

Could you tell me how these function from C# can be re-written to C++ via Qt lib?

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]);
        }
    }

1 Answer 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);
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.