c++ - Append multiple hex numbers to QByteArray simultaneously -


i have bunch of hex numbers, don't feel doing

qbytearray ba; ba.append(0x01); ba.append(0x02); ba.append(0x7a); ... 

can in 1 line? maybe qstring manipulation?

i'm sending messages via serial communication qextserialport , need store hex commands in qbytearray can use qint64 write(const qbytearray &data)

the first point is: there no such thing hex numbers. numbers numbers. we're talking of integers here. whether 16, 0x10, 020, etc., it's same number. 0x10 "hex" in sense write out in hexadecimal notation. doesn't change number itself, doesn't make "hex"! it's still number incrementing 0 sixteen times.

the reason hexadecimal device's documentation gives command packets in hexadecimal. it's arbitrary choice. can copy numbers in hexadecimal documentation, or convert them base if makes more sense you.

so, hexadecimal representation of course you, don't need explicitly use it. need use c constant arrays in fashion - make things simple , low-overhead. suppose command consist of 3 bytes decimal values 16, 33, 47.

you encode follows:

static const char cmdbuf[] = { 16, 33, 47 }; const auto cmd = qbytearray::fromrawdata(cmdbuf, sizeof(cmdbuf)); 

the byte array cmd initialized without copying data, that's fast , efficient. the cmdbuf must static since must exist long cmd or of (shallow) copies do.

you use string literal initialize array, using hexadecimal escapes non-printable characters, , printable characters otherwise.

static const char cmdbuf[] = "\x10!/"; const auto cmd = qbytearray::fromrawdata(cmdbuf, sizeof(cmdbuf)-1); 

the reason sizeof(...)-1 cmdbuf 4 bytes long - string literal ends terminating 0 don't need.

if wished use hex representation bytes, have

static const char cmdbuf[] = "\x10\x21\x2f"; const auto cmd = qbytearray::fromrawdata(cmdbuf, sizeof(cmdbuf)-1); // or static const char cmdbuf[] = { 0x10, 0x21, 0x2f }; const auto cmd = qbytearray::fromrawdata(cmdbuf, sizeof(cmdbuf)); 

of course use octal well:

static const char cmdbuf[] = "\020\041\057"; const auto cmd = qbytearray::fromrawdata(cmdbuf, sizeof(cmdbuf)-1); // or static const char cmdbuf[] = { 020, 041, 057 }; const auto cmd = qbytearray::fromrawdata(cmdbuf, sizeof(cmdbuf)); 

or mix of them!

static const char cmdbuf[] = "\020\x21/"; const auto cmd = qbytearray::fromrawdata(cmdbuf, sizeof(cmdbuf)-1); //or static const char cmdbuf[] = { 16, 0x21, 057 }; const auto cmd = qbytearray::fromrawdata(cmdbuf, sizeof(cmdbuf)); 

in string literal, whether encode hex/octal escapes or use printable characters you, it's matter of style. if values in commands don't have meaning of printable characters, numerical (hex or octal) encoding in string literal or array initializer better.

when choosing between octal , hex, follow structure of command bytes or own preference. if bytes have structure somehow breaks down groups of 2+3+3 bits, octal way of making human-readable. otherwise, use hex or decimal. it's makes code easier read - machine doesn't care, binary output identical no matter way go it.


Comments

Popular posts from this blog

html - Firefox flex bug applied to buttons? -

html - Missing border-right in select on Firefox -

python - build a suggestions list using fuzzywuzzy -