If I have a number like '1234' how can I take a column like 1234 to save in variable ?
Where is the number? What data type?
Not sure if this is what you want:
void setup() {
char result[10];
char temp[10];
int val = 12345;
Serial.begin(9600);
memset(result, 0, sizeof(result)); // Set all to null
itoa(val, temp, 10); // Make val a string or simply
// initialize with string first
// and skip this step
strncpy(result, &temp[1], 1);
Serial.println(result);
}
void loop() {
}
econjack:
Not sure if this is what you want:void setup() {
char result[10];
char temp[10];
int val = 12345;
Serial.begin(9600);
memset(result, 0, sizeof(result)); // Set all to null
itoa(val, temp, 10); // Make val a string or simply
// initialize with string first
// and skip this step
strncpy(result, &temp[1], 1);
Serial.println(result);
}
void loop() {
}
Many thanks econjack, I'll try this code,
Thank you alot Mr econjack, your example very well, that's I was looking, it's work good with me! .
A numerical solution is much faster and uses much less memory
For your example:
int value = 1234;
value /= 100; // integer division gives 12
value %= 10; // modulo operation gives the last digit 2
You have to adjust the divisor 100 if you want other digits
olf2012:
A numerical solution is much faster and uses much less memory
For your example:int value = 1234;
value /= 100; // integer division gives 12
value %= 10; // modulo operation gives the last digit 2
You have to adjust the divisor 100 if you want other digits
Wow Mr olf2012 ! this is very good code , I using this in mikroC but I didn't know is it working in arduino !!
I'll using it , thanks alot again.
olf2012 solution will be faster. However, change his test value from 1234 to 12345 and note what happens. It would take a little more code to make it a general purpose function.
econjack:
olf2012 solution will be faster. However, change his test value from 1234 to 12345 and note what happens. It would take a little more code to make it a general purpose function.
I tested last code but not working ):
I tested last code but not working ):
If you tried to use the same code to get the second digit of 12345 as you did for 1234 then you are confirming what Jack warned you about. If that is not what you did then please post the code that failed and describe what failed.
Of course my solution is not universal. OP did not tell us which digit he wanted. If it is always the same digit counted from the right then a constant divisor can be used. If you want the nth digit from the right you have to calculate 10^n, preferably by using a for-loop and not the pow-function because the latter uses floating point arithmetic and will introduce errors
Everyone is trying to guess what the OP wants. That was the first question, never answered.
this is good working:-
void setup() {
char result[10];
char temp[10];
int val = 12345;
Serial.begin(9600);
memset(result, 0, sizeof(result)); // Set all to null
itoa(val, temp, 10); // Make val a string or simply
// initialize with string first
// and skip this step
strncpy(result, &temp[1], 1);
Serial.println(result);
}
void loop() {
}
but I ask if the other code var = ( temp/100)%10;
is working in arduino or not ?
Why not test it and find out?
aarg:
Why not test it and find out?
I tested with LCD then I see unknow icon !
Provided you use the same variable names you should use
(val/100)%10;
instead of
(temp/100)%10;
olf2012:
Provided you use the same variable names you should use(val/100)%10;instead of
(temp/100)%10;
yes I know, like this:-
int var;
char temp;
temp = ( temp/100)%10;
but not working!
yes I know, like this:-
int var;
char temp;temp = ( temp/100)%10;
but not working!
Of course not. But, since I do't anser question about incomplete code, and I don't answer questions for crossposters, you can just keep stabbing in the dark, instead of trying to actually learn.
wleed75:
yes I know, like this:-int var;
char temp;temp = ( temp/100)%10;
but not working!
I think that you need this table, here: ASCII table
Look at the table. Notice that the number 48 gets you the character '0'.
So if you want to display the character '0', you need to put the number 48 into your char variable. There are at least two ways to do this:
temp = 48; // one way
OR
temp = '0'; // another way (notice the single quotes)
If you are getting "strange icons" instead of digits, then you could try adding 48, like this:
temp += 48; // one way
OR
temp += '0'; // another way (the compiler will know what you mean)
Also, you should not try to put a large number (like 1234) into one char variable. It cannot hold numbers that big. (It only goes from -128 to +127.)
try a function like this:
uint32_t myNumber = 1234567890;
int position = 8;
void setup()
{
int result = extractDigit(myNumber, position);
Serial.println(result);
}
void loop()
{
}
int extractDigit(uint32_t value, int place)
{
while (place--)
{
value = value/10;
}
value = value%10;
return value;
}