Converting strings to 'real' numbers ?

As the subject, I think that’s what I want to do. Well, what I want to do is convert 9 bytes (from serial) to ‘real’ numbers to move a servo (in degrees) like this :-

W239 048 is the sort of 9 bytes, what I’m trying to do is extract the 239 and 048 to send to two servos (to azimuth and elevation) but can’t think how to do it ! :frowning: :-[


You can convert a character digit to a numerical digit using the following:

char charDigit = ‘3’;
int digit = charDigit - ‘0’; // this is the same as digit = 3;

Next note that the number 239 = 2 * 100 + 3 * 10 + 9

In general, in a base-10 number system, a number equals:

sum(digit n * 10 ^ n) from n = (num digits - 1) to 0.

From here you should be able to write a simple algorithm to turn a string of numerical characters into an integer.

  • Ben

Thanks, sounds good. I’m trying to make two servos ‘track’ the moon for example using software called Orbitron - it has a plug-in to control a ham radio az-el rotor and can output in the form WAAA EEE - AAA being the azimuth and EEE the elevation in degrees. Then I could send it to, two servos and also a bit of code to get over the 180 degree limit of servos

If (az=>180)
{tilt=180-el} ;

‘Tilt’ and ‘dir’ are to be sent to the servos and az and el from the coversion from WAAA EEE


The following is an excerpt from a program to interface with an RS232 controlled instrument, which returned a string, of which the numerical data was extracted to be passed to a D2A function:


Serial.println(“MEAS POW L1 D2?”); //RS232 Query.
int i = 0; //loop integer.
int X = 0;
while (X != 58){
if (Serial.available() > 0) { //Check serial buffer for info.
X =; //Read byte from serial buffer.
SERIALDATA = X; //Store byte to string.

  • i++; //Go to next byte.*
  • }*
  • }*
  • long P1 = SERIALDATA[0]-48;*
  • long P2 = SERIALDATA[1]-48;*
  • long P3 = SERIALDATA[2]-48;*
  • long P4 = SERIALDATA[4]-48;*
  • long P5 = SERIALDATA[5]-48;*
    _ long POWER = 10000P1+1000P2+100P3+10P4+P5;_
  • return POWER;*
    Essentially it just reads each ascii character as its byte reference and then subtracts 48 to get the actual number. The numbers then need to be recombined.

Ah thanks :slight_smile: I just needed the right pointer :wink:

Just curious what does the ‘RS232 controlled instrument’ do ?


RS232 is the standard serial interface on your computer,

It’s like USB in a way,

Certain number of data pins, ad expectations for how they’re triggered to send data.

I knew what RS232 was, just curious what type of RS232 controlled instrument Spankyham was using - Thanks anyway.