Casting help

Hi, I'm trying to set bits in clockData{}, which is a byte array. binStr is a string of 1's and 0's that will set the bits. How can I cast the string, binStr, to byte? Thanks in advance for the help.

 for (int index = len; index>=0; index-- ) {
   strncpy(clockData[pos],(binStr+index),1);
   pos++
  }

binStr is a string of 1's and 0's that will set the bits. How can I cast the string, binStr, to byte?
binStr is a string of '0' and '1' characters, not a collection of bits.

You need to find a different approach. strncpy is not how you set bits in bytes.

Thanks for the reply. I thought it would be easiest to change clockData[] to a char array, but the error now is, "Invalid conversion from "char" to char*" Can you suggest a better way to do this? Thanks again in advance.

strncpy wants to copy some number of characters from one array to another. clockData is now an array of chars, but but clockData[pos] is not and (binStr+index) is not. (binStr+index) is a character, as is clockData[pos].

You can directly assign one character to another:

clockData[pos] = (binStr+index);

binStr contains a binary number up to four positions, such as "0100". Each byte needs to be separated in the array, as:

clockData[10] = "0"
clockData[11] = "0"
clockData[12] = "1"
clockData[13] = "0"

I'm still checking, but I don't see a way to do this. I'd rather keep clockData[] as a byte array, but string is ok if necessary. Is there something similar to STR(, MID(, etc., in basic? Thanks again.

binStr contains a binary number up to four positions, such as "0100"

OK.

I'm still checking, but I don't see a way to do this. I'd rather keep clockData[] as a byte array

OK.

byte clockData[4];
clockData[0] = binStr[0] - '0';

Hi,
I would do something like this:

for( int i=0; i<strlen(binStr); ++i)
{
   if( binStr[i] == '0' )
      clockData[i] = 0;
   else
      clockData[i] = 1;
}

MitchSF:
binStr contains a binary number up to four positions, such as "0100". Each byte needs to be separated in the array, as:

clockData[10] = "0"
clockData[11] = "0"
clockData[12] = "1"
clockData[13] = "0"

I'm still checking, but I don't see a way to do this. I'd rather keep clockData[] as a byte array, but string is ok if necessary. Is there something similar to STR(, MID(, etc., in basic? Thanks again.

Where are you getting binStr from? And what uses ASCII characters to work with bits?

clockData[10] = "0"
clockData[11] = "0"
clockData[12] = "1"
clockData[13] = "0"

wrong - wrong - wrong - and it makes me wonder about the rest

1st of all if clockData[] is a byte array then it would be

clockData[10] = '0';

where '0' is an signed 8-bit ASCII character that would get cast to an unsigned 8-bit byte.

You want to read or set bits, try using the bits and bytes commands on the reference page here;

make sure to read try them all out to check your understanding. You know, collect the whole set!

Disclaimer: only tested with gcc on a normal PC (with unsigned char instead of byte), no Arduino tests.

byte binStrToByte(const char* str) {
    int i;
    byte result = (byte)0;

    for (i = 0; i < 8; i++) {
        result |= (str[i] - '0') << (7 - i);
    }   
    
    return result;
}

This function would turn for example "10010001" into 0x91.

HTH