Pages: [1]   Go Down
Author Topic: How to convert ascii to decimal  (Read 1824 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 14
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am doing some serial communication and I am having trouble converting ascii to decimal anyone got any ideas.
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 610
Posts: 49086
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

There are several ways to convert strings to numbers.

Decimal is a C# type. It is not a C/C++ type, so, you will not be able to convert a string to a decimal type.

If you meant some other type, you could capture the whole string (NULL terminated), and use atoi, atol, or atof to convert the string to a integer, long, or float, respectively.

If you want to convert the incoming data to an integer, you can do it receiving one character at a time. Suppose the character stream contains '1', '7', '0', and '9'. Create an int variable, val, and set it to 0.

After reading a character, multiply val by 10 and add the character - '0'.

val = 0. Read the '1'. val * 10 = 0. val + '1' - '0 = 1.
val = 1. Read the '7'. val * 10 = 10. val + '7' - '0 = 17.
val = 17. Read the '0'. val *10 = 170. val + '0' - '0' = 170.
val = 170. Read the '9'. val * 10 = 1700. val + '9' - '0' = 1709.
Logged

Pages: [1]   Go Up
Jump to: