convert char array into byte, then byte to int


I need to convert a char array into byte and then ack to an int value that i can read it in my arduino serial monitor. i couldn't figure it out.

void setup(){

void loop(){
  char btes[]={0,0,0,0,0,0,1,1};

  byte b=btes[?]; //here is my problem. how do i read the whole array

  int numbers=b;

thanks for any help

Are these...

int btes[]={0,0,0,0,0,0,1,1};

...bits that you are trying to combine into a byte?

yes exactly. i'm trying to read this array as a byte

Unless you have a reason for storing the bit values in an integer array, change the array type to byte

byte btes[]={0,0,0,0,0,0,1,1};

You can do it the “long” way…

  byte b=
    (btes[0] << 7) |
    (btes[1] << 6) |
    (btes[2] << 5) |
    (btes[3] << 4) |
    (btes[4] << 3) |
    (btes[5] << 2) |
    (btes[6] << 1) |
    (btes[7] << 0);

Or with a for loop…

  byte b = 0;
  for ( byte i=0; i < 8; ++i )
    b |= (btes[i] << (7-i));

Note: I assumed btes[0] is the “high” bit.

thats exactly what i needed. its working now. thank you so much :)

@Coding Badly please tell me that how can i change the type variable or you can say type casting? I am taking an analog value and wanted to convert into byte. Please tell me ass soon as you can

I am taking an analog value and wanted to convert into byte

An analogue value is normally 10 bits, so won't fit into a byte. You could use "map (analogueValue, 0, 1023, 0, 255);" or more simply "analogueValue >>= 2;"

@AWOL thank you sir. So nice of you