How to change byte to int? - Arduino I have searched everywhere, but can you please help me to get this Byte into int, there are alot of examples on the internet, but not for a beginer like me

CODE:

int Button[] = {2, 4, 7 };

byte E = sizeof(Button);

int I = (int) E;


void setup() {

  Serial.begin(9600);

  Serial.print (I); // has to be 3, currently 61


  }


void loop() {

 }


I don't know why the byte E doen't convert to a int in int I by using the  (int)E command, what would also be convinient would be to read the amount of elements directly  as an int .
(you might wonder, why I want to do this, it is beacause, I am writing a program with multiple buttons, so I just want to add another pin to the array and not have to change the "I" int( the I int is used for loops))
The code works correctly. The function sizeof() returns the number of bytes in a variable, not the number of elements. Your array is of type int, which consists of 2 bytes each. So in sum you get 6 bytes for the whole array.

What you want to do (calculating the number of elements in the array) is to divide the number of bytes in the array by the number of bytes for one element:

int I = sizeof(Button)/sizeof(Button[0]);


The conversion from byte to int worked correctly, but you really don't need it. You can calculate things with both types the same way. The only difference is the value range. Since a negative element number doesn't make sense and you surely don't want to define more than 255 buttons, you can choose yourself, what you want to use. Using byte would save you one byte of memory.