Convert function to qt notation
-
Hi,
Please help how convert this line: buffer.writeBigInt64LE(BigInt(number)) to qt ?
JS:function int64LE(number) { const buffer = Buffer.alloc(8) buffer.writeBigInt64LE(BigInt(number)) return buffer }qt:
QByteArray int64LE(quint64 number) { QByteArray buffer[8]; ...... return buffer; } -
@Christian-Ehrlicher
Still doesn't work:QByteArray qint64_2_LE(qint64 val) { QByteArray le; le.resize(8); qToLittleEndian(val, &le); return le; }Application crashed after call qToLittleEndian function.
@Damian7546 said in Convert function to qt notation:
qToLittleEndian(val, &le);
qToLittleEndian writes the result into the memory block the second parameter points to. And you're passing pointer to QByteArray!
It should be:qToLittleEndian(val, le.data()); -
Why manually when there are helper functions?
https://doc.qt.io/qt-6/qtendian.htmlOh, now deleted for unknown reasons.
-
Maybe you can ask chatgpt.....
This answer is from deepseek:To convert the JavaScript function
int64LEto Qt, you need to create aQByteArraythat holds the little-endian byte representation of a 64-bit signed integer. Here's the step-by-step solution:- Resize the QByteArray to hold 8 bytes (64 bits).
- Cast the input
quint64number toqint64to handle it as a signed integer. - Extract each byte in little-endian order by masking and shifting the value.
- Store each byte in the QByteArray.
QByteArray UtilsEssp::int64LE(quint64 number) { QByteArray buffer; buffer.resize(8); // Allocate 8 bytes qint64 value = static_cast<qint64>(number); // Treat as signed // Extract each byte in little-endian order for (int i = 0; i < 8; ++i) { buffer[i] = static_cast<char>(value & 0xFF); value >>= 8; } return buffer; }Explanation:
- Resizing the Buffer:
buffer.resize(8)ensures theQByteArrayhas space for 8 bytes. - Casting to Signed:
static_cast<qint64>(number)converts the unsigned input to a signed 64-bit integer, preserving the bit pattern. - Little-Endian Extraction: The loop iterates 8 times, each time taking the least significant byte using
value & 0xFF, then right-shiftingvalueby 8 bits. This fills theQByteArrayfrom the least significant byte to the most significant, achieving little-endian order.
This approach mirrors the original JavaScript functionality by directly manipulating the byte representation, ensuring compatibility with systems expecting a signed 64-bit integer in little-endian format.
-
D Damian7546 has marked this topic as solved on
-
Why manually when there are helper functions?
https://doc.qt.io/qt-6/qtendian.htmlOh, now deleted for unknown reasons.
@Christian-Ehrlicher said in Convert function to qt notation:
Why manually when there are helper functions?
https://doc.qt.io/qt-6/qtendian.htmlOh, now deleted for unknown reasons.
One way I use:
QByteArray m_data = QByteArray("\x0c\x06\x04\x04\x02\x00\x02\x00",8); qint64 value = qFromLittleEndian<qint64>(m_data);but how change value to m_data using QtEndian and function qToLittleEndian ?
will the following be ok?
QByteArray m_data; qToLittleEndian(value, &m_data ); -
You should resize the QByteArray before
-
You should resize the QByteArray before
This post is deleted! -
D Damian7546 has marked this topic as solved on
-
You should resize the QByteArray before
@Christian-Ehrlicher
Still doesn't work:QByteArray qint64_2_LE(qint64 val) { QByteArray le; le.resize(8); qToLittleEndian(val, &le); return le; }Application crashed after call qToLittleEndian function.
-
@Christian-Ehrlicher
Still doesn't work:QByteArray qint64_2_LE(qint64 val) { QByteArray le; le.resize(8); qToLittleEndian(val, &le); return le; }Application crashed after call qToLittleEndian function.
@Damian7546 said in Convert function to qt notation:
qToLittleEndian(val, &le);
qToLittleEndian writes the result into the memory block the second parameter points to. And you're passing pointer to QByteArray!
It should be:qToLittleEndian(val, le.data()); -
D Damian7546 has marked this topic as solved on