Skip to main content

blob.swap2()

Changes the order of the blob’s bytes

Availability

Device + Agent

Returns

Nothing

Description

This method changes the order of the contents of the target blob by reversing the order of every two bytes: bytes 0 and 1 become bytes 1 and 0, bytes 2 and 3 become bytes 3 and 2, and so on:

If each pair of bytes forms a 16-bit integer value, swap2() allows you change the ‘endian’ nature of this value: ie. whether its most-significant byte comes first or last. The imp is little-endian, but not all devices are, and this method allows you to interpret readings from them correctly, or to ensure that data sent to them is correctly formatted.

Example Code

local aBlob = blob(8);
for (local i = 0 ; i < 8 ; i++) {
aBlob.writen(i, 'c');
}
// aBlob is '00 01 02 03 04 05 06 07'
aBlob.swap2();
// my_blob is now '01 00 03 02 05 04 07 06'