gpt4 book ai didi

qt - 通过 Qt 库实现从 C# 到 C++ 的函数

转载 作者:行者123 更新时间:2023-12-04 05:25:24 25 4
gpt4 key购买 nike

你能告诉我如何通过 Qt 库将 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]);
}
}

最佳答案

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

关于qt - 通过 Qt 库实现从 C# 到 C++ 的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13250928/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com