Convert a string of binary numbers to binary.

Hello i have a string for example of "1010101" and i want to convert it and store it into a int[] as binary, ¿how can i do this?

for i example i want that string to have the same effect as when i assign this:

int bytes[10]; 

...
bytes[i] = B1010101;
...

//i need something like
byte[i] = Convert (string of"1010101")

Any help would be appreciated.

Have a look at the bitWrite function in the reference.

If you really have a string, you can determine the number of characters in the string, using strlen().

Then, a little math, a for loop, and bitWrite() will do the trick.

Sorry i confused i have a int[] not a string. And i want store it as "B1010101" in another int[]. I guess it would be similar. I would take it a look at that function.

Apart from in Java, I'm not sure what an "int[]" is.
Are you working in Java?

I made this but i think is not good.

 int contador = 0;

    for (int i = 0 ; i <343 ; i++)
    {
        if ( i !=50 && i !=100)
        {
         int tempbyte;

            for (int j = 0 ; j <7 ; j++)
            {
      
            bitWrite(tempbyte , j, sequence[j+contador]);
       
             }    

        bytes[i] = tempbyte;     

        }
       contador +=7;
   }

but i think is not good.

You think correctly.

How about a complete program that actually compiles, properly indented? That crap that wanders all over the damned place is beyond too hard to read. Is Tools + Auto Format THAT difficult to use?

Apart from in Java, I'm not sure what an "int[]" is.

Sorry again. I confused syntaxis with C#. I meant i have int arrays.

Tools + Auto Format THAT difficult to use?

Thank you for the tip :sunglasses:

How about the following?

  char* pEnd;
  char szNumber[] = "1101110100110100100000";
  byte val = strtol(szNumber, &pEnd, 2);

This may not be what you wanted, but its what you asked for.

// put a character string of zeros and ones into an int array (16 zeros and ones per int). 

void convert(char *stringIn, unsigned int *arrayOut) {
  int bit = 1;
  *arrayOut = 0;

  while(*stringIn) {
    if(*stringIn++ == '1')
      *arrayOut |= bit;

    if(!(bit <<= 1)) {
      bit = 1;
      *++arrayOut = 0;
    }
  }
}

Thank you Whandall, That worked!!! =)