Forum Menu




 


Log in Problems?
New User? Sign Up!
AVR Freaks Forum Index

Post new topic   Reply to topic
View previous topic Printable version Log in to check your private messages View next topic
Author Message
LevenEigenaar
PostPosted: Feb 09, 2012 - 09:31 AM
Hangaround


Joined: Oct 13, 2011
Posts: 165


Hi i have solved the problem.

I have another queastion.

I have a buffer from example 10, how can I put the buffer[5] to a int?

example 12345678 5 is the number for the int
 
 View user's profile Send private message  
Reply with quote Back to top
JohanEkdahl
PostPosted: Feb 09, 2012 - 09:48 AM
10k+ Postman


Joined: Mar 27, 2002
Posts: 18749
Location: Lund, Sweden

Code:
int i = buffer[4] - '0';
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
LevenEigenaar
PostPosted: Feb 09, 2012 - 09:58 AM
Hangaround


Joined: Oct 13, 2011
Posts: 165


Thank you!


Why do you - 0 ??

Grz
 
 View user's profile Send private message  
Reply with quote Back to top
LevenEigenaar
PostPosted: Feb 09, 2012 - 10:11 AM
Hangaround


Joined: Oct 13, 2011
Posts: 165


Hi,

It sends none char to my int with your code?!
 
 View user's profile Send private message  
Reply with quote Back to top
JohanEkdahl
PostPosted: Feb 09, 2012 - 10:27 AM
10k+ Postman


Joined: Mar 27, 2002
Posts: 18749
Location: Lund, Sweden

Quote:

Why do you - 0 ??

I don't. I do - '0'.

There is a principal difference between 0 and '0'. The character '0' has ASCII value 48, '1' is 49 and so on.

So by taking any character '0' to '9' and subtract 48 you get the numerical value of the digit character. Using '0' or 48 to subtract does not matter, technically they are identical. But for keeping the code readable I think '0' is neater.

Quote:
It sends none char to my int with your code?!

Show minimal, but complete (as in buids and runs), test program.
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
LevenEigenaar
PostPosted: Feb 09, 2012 - 03:56 PM
Hangaround


Joined: Oct 13, 2011
Posts: 165


Okay.

I understand what you mean. But for me it is not entirely clear.

If I get a string from the rs232 for example 3636. How can I use the number 3 as int? So I, for example, the arithmetic value 3 can do 1000 times

I want to use real timer from a server, the server sends 3450 that means 2 hours evening. That I bear the clock in the Atmega can recover. If the time expires.
 
 View user's profile Send private message  
Reply with quote Back to top
JohanEkdahl
PostPosted: Feb 09, 2012 - 04:07 PM
10k+ Postman


Joined: Mar 27, 2002
Posts: 18749
Location: Lund, Sweden

Quote:
If I get a string from the rs232 for example 3636. How can I use the number 3 as int?

I thought I was clear.. Assuming that the string "3636" is in a variable called s, then getting the numerical value of the first digit can be done
Code:
int value = s[0];

and for the third digit
Code:
int value = s[2];


Quote:
So I, for example, the arithmetic value 3 can do 1000 times

I want to use real timer from a server, the server sends 3450 that means 2 hours evening. That I bear the clock in the Atmega can recover. If the time expires.

I am truly sorry, but I can not understand this at all.

What do you mean by "arithmetic value 3 can do 1000 times"? Could you express this in another way?

What makes "3450" map to "2 hours evening" (which I read as "2 o'clock in the afternoon", i.e. "2 PM")?


Last edited by JohanEkdahl on Feb 09, 2012 - 04:13 PM; edited 1 time in total
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
clawson
PostPosted: Feb 09, 2012 - 04:09 PM
10k+ Postman


Joined: Jul 18, 2005
Posts: 62922
Location: (using avr-gcc in) Finchingfield, Essex, England

I already told you about atoi()?!? What that does is convert each digit it comes to from ASCII to binary (by subtracting 48) then it adds that into a running total and if there are more digits to go it multiplies the total by 10. So for "3636" it would read the '3', subtract 48 to convert '3' into 3 and add it to the total (0 at the start) so the total is 3. There are more digits to go so it multiplies the total by 10 to make it 30 then read the next digit which is '6'. It subtracts 48 to convert '6' into 6 then adds this to the total. As it was 30 it is now 36. There are more digits so it multiplies by 10 to get 360 then reads '3', converts to 3 then adds to the total to get 363. Finally it does the same for 6 so the total is 3636 - which is what you want.

So why do this yourself? Why not use atoi() like I told you?

_________________
 
 View user's profile Send private message  
Reply with quote Back to top
JohanEkdahl
PostPosted: Feb 09, 2012 - 04:18 PM
10k+ Postman


Joined: Mar 27, 2002
Posts: 18749
Location: Lund, Sweden

Oh so that is what is wanted?
Convert a string consisting of numerical characters to an integer value?

Yes, atoi().
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
LevenEigenaar
PostPosted: Feb 09, 2012 - 06:11 PM
Hangaround


Joined: Oct 13, 2011
Posts: 165


Okay I get it a little bit what you mean.

@ JohanEkdahl

I have 2 counters running (server) and (microprocessor) set as the source voltage of the ic is too low, the counter in the ic ward no longer be installed. The server ensures that 2 times a day, a string is sent to the ic to the counter in the ic again correctly set.

What I mean by the time

The numerator is the time, for example,

1day = 14400 minutes (14400/3) = value of counter in ic is 480

So for example, 480 is 00.00 hours
240 is so for example 12.00

7 days = 100800 minutes (100800/3) = value of counter ic is 33600

The same counter is also in the server. Set the accumulator of the ic is empty, then the counter is no longer increase. If the ic is re-inflated, the server will reset the ic equate the numerator of the server.

/ / --------------------------------

So I get off of the server instance, the time 480 (00.00) hours

The ic 480 but does not see (4) (Cool (0) separately

So I want the 4 for example convert an int, and multiplying by 100

Same story as 8 multiplied by 10

And multiplying, for example, with 0 0

Together on beats and you have the same value.

But how can I buffer so for example [1] (This is the value 4) equate to an int?



Thank you for your time, regards
 
 View user's profile Send private message  
Reply with quote Back to top
clawson
PostPosted: Feb 09, 2012 - 06:18 PM
10k+ Postman


Joined: Jul 18, 2005
Posts: 62922
Location: (using avr-gcc in) Finchingfield, Essex, England

Quote:

But how can I buffer so for example [1] (This is the value 4) equate to an int?

Are you listening or not?
Code:
char input = "3450";
int n;
n = atoi(input);

Now n contains the numeric value 3450.

_________________
 
 View user's profile Send private message  
Reply with quote Back to top
LevenEigenaar
PostPosted: Feb 09, 2012 - 06:23 PM
Hangaround


Joined: Oct 13, 2011
Posts: 165


Yes I understand that but you designate a default value of 3450, that variable must be!

That I from the server each time a different value from sending messages.

So, for example 3450 or 3451 or 3452

Or did I do wrong now?
 
 View user's profile Send private message  
Reply with quote Back to top
LevenEigenaar
PostPosted: Feb 09, 2012 - 06:27 PM
Hangaround


Joined: Oct 13, 2011
Posts: 165


, Or 3450 is the maximum value?

char input = "3450", / / char up to 3450 so it can also be 33600
int n, / / ​​int value can be so 3
n = atoi (input) / / input buffer [1] For example, this would be the value 3?!

Am I doing it right?
 
 View user's profile Send private message  
Reply with quote Back to top
LevenEigenaar
PostPosted: Feb 09, 2012 - 06:50 PM
Hangaround


Joined: Oct 13, 2011
Posts: 165


Code:
/* convert a string to an integer */
   #include <stdio.h>
   #include <stdlib.h>

   main()
   {
      int sum;
      sum = atoi(buffer[1]);

   }
/

For example, buffer [4] is number 3

Then the int also 3?
 
 View user's profile Send private message  
Reply with quote Back to top
sternst
PostPosted: Feb 09, 2012 - 06:59 PM
Raving lunatic


Joined: Jul 23, 2001
Posts: 2467
Location: Osnabrueck, Germany

LevenEigenaar wrote:
Code:
/* convert a string to an integer */
   #include <stdio.h>
   #include <stdlib.h>

   main()
   {
      int sum;
      sum = atoi(buffer[1]);

   }
/

For example, buffer [4] is number 3

Then the int also 3?
No.
The solution for a single digit was already handed to you by Johan.
Code:
sum = buffer[4] - '0';
And if you are actually getting numbers over the serial line instead of characters, then the solution is
Code:
sum = buffer[4];

_________________
Stefan Ernst
 
 View user's profile Send private message  
Reply with quote Back to top
LevenEigenaar
PostPosted: Feb 09, 2012 - 07:13 PM
Hangaround


Joined: Oct 13, 2011
Posts: 165


Okay, but why is that piece of script to apply?

So I must do the following

int test;

test = buffer [4];

test = test * 1000;

This must just work?

Buffer [4] can be different for each buffering
 
 View user's profile Send private message  
Reply with quote Back to top
LevenEigenaar
PostPosted: Feb 09, 2012 - 07:24 PM
Hangaround


Joined: Oct 13, 2011
Posts: 165


Hey guys

I do not this piece?! Can you explain this to me. Why there - '0 '; is used?

sum = buffer [4] - '0 ';
 
 View user's profile Send private message  
Reply with quote Back to top
sternst
PostPosted: Feb 09, 2012 - 07:24 PM
Raving lunatic


Joined: Jul 23, 2001
Posts: 2467
Location: Osnabrueck, Germany

LevenEigenaar wrote:
This must just work?
It depends on what actually is in buffer[4]. Is it a binary number, or is it a single character representing a single digit of a number?

_________________
Stefan Ernst
 
 View user's profile Send private message  
Reply with quote Back to top
LevenEigenaar
PostPosted: Feb 09, 2012 - 07:35 PM
Hangaround


Joined: Oct 13, 2011
Posts: 165


Yes you understand what I mean! sternst


It is NOT a binary but a sign! That's the problem. And thus would like to convert data to binary
 
 View user's profile Send private message  
Reply with quote Back to top
DO1THL
PostPosted: Feb 09, 2012 - 08:20 PM
Resident


Joined: Aug 29, 2002
Posts: 790
Location: Muenster, Germany

Boy (LevenEigenaar),

before you do a single keystorke again here, please think (!!!) twice about the difference of a numeric value and a printable character. There's a huge difference between a 3 and a '3', which you simply don't seem to recognize. And that huge difference is that the number 3 (a number is something a processor can do calculations with) has the value 3, whereas the character '3' (something you will see at your terminal as a single printed character) is something that visibly represents the number 3 in a way humans can read.

Once you got that, then you might understand that the difference between a numerical value 0, 1, 2, 3... and the printed character '0', '1', '2', '3'... is simply a fixed offset of 48 (decimal). Since any electronic device needs to find a way to represent printable characters, some coding rules were invented. The coding rule applicable here is called ASCII, a large table that for each printable (and some non-printable ones) character shows the numeric representation when being used in a digital system. So you will find that a numeric value 3 has the numeric value of 3 (who wonders), and the printable character '3' according to the ASCII coding scheme has the value of 51. To get back and forth between numeric values and their printable representation, just ad or subtract this magic 48!

Once you got that, re-read the last dozen replies to your questions, and a sudden enlightenment will shine upon you!

_________________
Einstein was right: "Two things are unlimited: the universe and the human stupidity. But i'm not quite sure about the former..."
 
 View user's profile Send private message  
Reply with quote Back to top
Display posts from previous:     
Jump to:  
All times are GMT + 1 Hour
Post new topic   Reply to topic
View previous topic Printable version Log in to check your private messages View next topic
Powered by PNphpBB2 © 2003-2006 The PNphpBB Group
Credits