Hey Creators,
I want to know if I can print or assign integer value by indexing like array.
Ex.: m=56;
c=m[0];
b=m[1];
Thank You!
Hey Creators,
I want to know if I can print or assign integer value by indexing like array.
Ex.: m=56;
c=m[0];
b=m[1];
Thank You!
No. c=m/10; b=m%10
An integer is not a string and does not have an array index reference as a simple type.
thedarknight:
I want to know if I can print or assign integer value by indexing like array.
of course you can set the value of a integer to the value of an array element.
but "m" is not an array. you could translate it to a string and then set "chars" to specific chars in the string using an index
int m = 56;
char s [10];
sprintf (s, "%d", m);
char c = s [0];
char b = s [1];
char t [20];
sprintf (t, " c %c, b %c", c, b);
Serial.println (t);
another method if you don't mind indexing from right to left...
// function to extract a single digit from an int
uint8_t extract( long num, uint8_t index, uint8_t base = 10 )
{
return static_cast<uint8_t>( num / pow(base, index) ) % base;
}
void setup()
{
Serial.begin(115200 );
int m=56;
Serial.println( extract( m, 1 ) );
Serial.println( extract( m, 0 ) );
Serial.println();
Serial.println( extract(0xABCD, 2, HEX ), HEX ); // just to demonstrate using a different number base
}
void loop() {}