Go Down

### Topic: How to convert ascii to decimal (Read 3852 times)previous topic - next topic

#### house91320

##### Aug 29, 2010, 02:35 am
I am doing some serial communication and I am having trouble converting ascii to decimal anyone got any ideas.

#### PaulS

#1
##### Aug 29, 2010, 02:51 am
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.

Go Up

Please enter a valid email to subscribe