Creating a string from binary


I have an application with a number of rows of 16 buttons and sliders in several arrays. The idea is to have each button value be represented by turning on or off a bit in a bitfield. For example, if there are 16 buttons in a row, and only the last button in the row is enabled, then I want to create a binary word


Upon pressing button or other trigger, the binary word that is derived from the row of buttons and their value status (on/off). If the last two buttons are on, then the word result will be 0000000000000011

I will OR all 16 button value states to get the final row state that will be sent out to up to 16 devices, this will be sent as a string out of a usb port.

The thing is, there are various cases where more or less data will be sent and parsed at the receiving end. There will be an opcode byte that preceeds the other data. For example I may want to send out an opcode followed by 16 bits representing a certain button status followed by another row of button value status, an example shown here:

00000001 opcode byte 1
0000000000000011 button1 value state as bitfield 2bytes
1111000011110000 button2 value state as a bitfield 2 bytes
01010101 DAC1 value 1 byte

I can construct the binary values and dac values and opcode. I understand that in xojo I cannot just simply send out a hex value to my microprocessor, which would be preferred. Rather I must create a string, such that the micro will then need to parse the string and reconvert to hex for it’s own use.

Here is the method of sending the string:
TxData is a global property as string

if TXdata <> “” then
// Send the response
mPort.Write TXdata
end if

Assuming that it is true that I cannot just send out the data in the raw binary state or as hex, can someone please advise me on how to construct a packet fro the example above so that I can send it with the method shown?

Any help appreciated.

BTW to clarify, there may be various ways the output is configured, in some cases maybe just a single opcode byte, other cases multiple bytes + words. The opcode will tell the receiving micro how to dissect the complete packet.

Create a MemoryBlock of the number of bytes needed to contain your data.You can then set individual bytes using MemoryBlock.Byte, and MemoryBlocks can be used anywhere a String is expected.

Wow that is perfect! Many thanks. Couldn’t be simpler.