Home > Blockchain >  atoi(happens with strtol as well) why add in the array '0'?
atoi(happens with strtol as well) why add in the array '0'?

Time:10-07

Im trying to study a code that has

 array[0] = digitalRead(pin1);
 array[1] = digitalRead(pin2);
 array[2] = digitalRead(pin3);
 array[3] = digitalRead(pin3);
 array[4] = digitalRead(pin4);
 array[5] = digitalRead(pin5);
 array[6] = digitalRead(pin6);
 array[7] = digitalRead(pin7);

 for(i=0; i<8 ; i  ){
  data[i] = array[i]   '0';
 }
 
 input = atoi(data);
 

im curious why did they add a '0'? when i try to run the code without the '0' it returns 0 which i assume is saying it can't be converted

CodePudding user response:

Short answer: '0' is added to convert interger values to ascii character values.

Explanation:

It's important to know that integer values like 0, 1, 2, ... are not the same as characters like '0', '1', '2', ... Characters do have an integer value that are defined in ascii-tables, see https://en.wikipedia.org/wiki/ASCII, but that value differs from the integer value. For instance the character '0' has the integer value 48. So to convert between an integer value (less than 10) and the corresponding character. There need to be some "conversion" - see later.

For your code:

digitalRead(pin1) returns an integer value being either 0 or 1

The purpose of the for loop is to generate a string that represents the value of the 8 pins. For instance like "10010110".

And finally the atoi call is to convert the string to an integer value. For instance converting the string "10010110" to the integer value 10010110 (decimal).

In order to construct the string from integer values that are 0 or 1, you need to calculate the integer value that represents the characters '0' and '1'. If you look-up ascii values, e.g. https://en.wikipedia.org/wiki/ASCII#Printable_characters , you can see that the character '0' has the decimal integer value 48 and the character '1' has the decimal integer value 49. So to go from integer value 0 to character '0' you need to add 48. Likewise - to go from integer value 1 to character '1' you need to add 48. So the code could be:

data[i] = array[i]   48;

However, in C a character is considered an integer value. So instead of writing 48, C allows you to simply write the character that has the ascii-value 48. In other words:

data[i] = array[i]   48;

is the same as

data[i] = array[i]   '0';

The compiler will automatically convert '0' to 48.

BTW: Make sure that data is defined as (at least) a 9 character array and that data[8] is already zero. Like char data[9] = {0};

That said... if array and data isn't used in other places, it seems a strange and complex way to calculate input. An alternative could be:

 input = 0;
 input = 10 * input   digitalRead(pin1);
 input = 10 * input   digitalRead(pin2);
 input = 10 * input   digitalRead(pin3);
 input = 10 * input   digitalRead(pin3);
 input = 10 * input   digitalRead(pin4);
 input = 10 * input   digitalRead(pin5);
 input = 10 * input   digitalRead(pin6);
 input = 10 * input   digitalRead(pin7);

and if the pins could be placed in an array the above could be placed in a simple and short for-loop

CodePudding user response:

The code shown is silly. Why are (presumed) values of 0-1 being stored to a not-null-terminated array (NOT a string) then passed to a function to do this? :

unsigned char input = 0;
input = (input << 1)   digitalRead(pin1);
input = (input << 1)   digitalRead(pin2);
input = (input << 1)   digitalRead(pin3); // << THIS IS ORIGINAL OP CODE
input = (input << 1)   digitalRead(pin3); // << THIS IS ORIGINAL OP CODE
input = (input << 1)   digitalRead(pin4);
input = (input << 1)   digitalRead(pin5);
input = (input << 1)   digitalRead(pin6);
input = (input << 1)   digitalRead(pin7);

/* input's value now 0 to 01111111 (0-127) as an integer value. */
/* User assumes responsibility for LSB <=> MSB ordering of pins */
  • Related