[Interest] QByteArray vs QString, arg, why is there no arg()?

Giuseppe D'Angelo giuseppe.dangelo at kdab.com
Wed Sep 18 14:50:44 CEST 2019


Il 18/09/19 13:16, Jason H ha scritto:
> What's the best way to zero-pad a QByteArray?
> What I want is QByteArray("%1").arg(6, 10, 10, '0')

Mostly it has to do with the fact that QByteArray is sitting between two 
worlds; on one side it's just a container of bytes, on the other side it 
has _some_ manipulation functions for ASCII-like strings. "Some" 
because, as you've noticed, stuff like the arg() convenience is missing.

If you really need a QByteArray you can work around this by e.g. using a 
printf-like function, what you're looking for is the "%010d" formatting.

Pseudocode:

int n = 123;
const char *format = "The number is %010d";
auto size = qsnprintf(nullptr, 0, format, n);

QByteArray result(size, Qt::uninitialized);
qsnprintf(result.data(), result.size(), format, n);


HTH,
-- 
Giuseppe D'Angelo | giuseppe.dangelo at kdab.com | Senior Software Engineer
KDAB (France) S.A.S., a KDAB Group company
Tel. France +33 (0)4 90 84 08 53, http://www.kdab.com
KDAB - The Qt, C++ and OpenGL Experts

-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 4329 bytes
Desc: Firma crittografica S/MIME
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20190918/f2d23f20/attachment.bin>


More information about the Interest mailing list