Parse Byte into 8 Bits

I have code below that is pulling a byte from a modbus which I need to now get the bits MSB to LSB. I cannot seem to find a way to make that happen. The end result will I will know which but is On and which is Off. After this I will want to be able to write back but that is another day.

            function readbool(slv, from, numcoils, func) {
                local endaddress = (numcoils/8)-1;
                            local reqblob = blob();

                            /* Construct the request */
                            reqblob.writen(slv, 'b');                                                                                               // Slave address
                            reqblob.writen(func, 'b');                                                                                            // Function code
                            reqblob.writen(swap2(from), 'w');                                                          // Start address
                            reqblob.writen(swap2(endaddress), 'w');                                            // End address
                            reqblob.writen(crc16(reqblob), 'w');                      // CRC

                            /* Collaborate and listen */
                            local rspblob = this.dataTxRx(reqblob);
                            checkForException(rspblob, func);

                            rspblob.seek(2);
                            local numbytes = rspblob.readn('b');
                            if(numbytes != (numcoils/8)) {
                                            throw "Unexpected response length: " + numbytes + " != " + numcoils;
                            }
            
                            server.log("readbool " + numbytes);

                            rspblob.seek(3);
                            local valblob = rspblob.readblob(2*numcoils);
                            valblob.swap2();
                            local valarr = [];
                            for(local i = 0; i < numbytes; i++) {
                                            valarr.append(valblob.readn('b'));
                            }

                            return valarr;
            }

Probably not the most elegant, but how about doing a bitwise & with 0x01, then 0x02, then 0x04 and so on to 0x80.

Untested:
local mult=0x01; local bit = []; for (local i=0; i++; i<8) { bit.append(mult & byte); mult = mult << 1; }

Thanks, this was my attempt before I saw your note
rspblob.seek(3); // First byte of packed coil data in response
local valarr = [];
local b;
for(local i = 0; i < numcoils; i++) {
if(i % 8 == 0)
b = rspblob.readn(‘b’);
valarr.append((b >> (i % 8)) & 1);
server.log("bit loop " + ((b >> (i % 8)) & 1))
}

and this resulted in all zeros on the output which cannot be right. So either the blob is wrong or the bit conversion is bad.

Another option, harnessing the squirrel array api:

`
// "coils" is the byte you want to decode
// "result" is an array of boolean values

local mask=[0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01]
local result = mask.map(function(element,value=coils){
  return (element&value)>0 })`