How to combine two received inputs and read it as one whole number in WINAVR?

By : NLed

I want users to enter 2 digits using c=ReceiveByte() command. For example, I want the user to do the following :

Enter 5
Enter 3
Output number 53 in ascii value on screen ( using hyperterminal )
Store number in a single array
Use that number for other loops etc.

My draft code is :

int c1[3];

Is this right ? or am I storing the 2 digits incorrectly ?

Thanks alot for your help !

By : NLed


For output, you don't need to modify c1[0] and c1[1] since these already contain the characters as entered. As your code stands you just need to make sure that c1[2] contains a valid character, e.g.

c1[2] = '\n';

BTW, if you need to get the entered number as an int:

int num = (c1[0] - '0') * 10   (c1[1] - '0');
By : Paul R

Since you seem to be receiving bytes from the Receivebyte() function, you ought to store them as bytes (unsigned char), not as integers, since using an integer to store a single byte wastes it's 3 bytes of memory.

Otherwise, your echo implementation should work - even though you might want to add guards against a user sending characters wich are non-numeric.

You will have to convert the ASCII Characters '5' and '3' (ASCII codes 53 and 51) to theire numeric value (subtract 48 from each as numbers range from 48 to 57 in the ASCII coding scheme)

This video can help you solving your question :)
By: admin