Electronic Circuits and Projects Forum



Converting Array to integer

  1. #1
    bryan bryan is offline

    Converting Array to integer

    Hello All:

    Would appreciate some help on converting the contents of an array to an integer. User enters data and it is stored into an array, but how can i convert the contents to an integer.

    Here is what I have for adding the data to the array, until either 3 characters are entered from 0-9 or until the escape or enter key.

    Say the user enters 1 and then enter, the array would be 100, or the user could enter 001 and then enter, the value is 1 but how to pull it out of the array so it is a number of 1. I suppose I could pad the array with FF and test each value in the array, and multiply the placeholders by 1,10,100 and add them together?. Any suggestions ?.



    Code :
     
           unsigned char i;
    	unsigned int array[3];
    	unsigned char x; 
     
     
     do {
               while(!DataRdyUSART());       //polls receive buffer for available data
               i = ReadUSART();              // read a single character from buffer
          		 if ( ('0' <= i) && (i <= '9') && (x >=0) )
                  {   
    					 array[x]=i-Ascii;  //  Add to array
    	 				 printf("%c",i,0);   //display on terminal;
    					 x--;               
    			  }
     
       }  
     
        while ( 13 != i && 27 != i);
    Last edited by bryan; 19th April 2012 at 01:47 AM.

  2. #2
    panic mode panic mode is offline
    what if he enters something else, string for example?
    0

  3. Thread Starter #3
    bryan bryan is offline
    Can't as the code only allows 0-9 to be to be entered, unless I am missing something?
    0

  4. #4
    panic mode panic mode is offline
    can integer be negative?

    if not you could do something like

    Code :
            unsigned char i;
    	//unsigned int array[3];
    	unsigned char x; 
            int temp;
     
     do {
               temp=0;  // init temp as zero
               while(!DataRdyUSART());       //polls receive buffer for available data
               i = ReadUSART();              // read a single character from buffer
          		 if ( ('0' <= i) && (i <= '9') && (x >=0) )
                  {   
    				temp=temp*10;  // adding digit, multiply temp by 10 and then... 
                                    temp=temp+ i - '0'; // add current digit               
    			  }
     
       }  
     
        while ( 13 != i && 27 != i);
    +1

  5. Thread Starter #5
    bryan bryan is offline
    Thank you very much panic mode. That did the trick. So simple and efficient, can get rid of the array.
    0

Tags
Similar Threads
Thread Starter Forum Replies Last Post
Converting from Audio/Video cables to a Coaxial cable Talligan General Electronics Chat 5 5th September 2003, 01:26 AM
Practically converting AC voltages Scubasteve Electronic Projects Design/Ideas/Reviews 5 9th April 2003, 01:05 PM
Converting floating ground audio to common ground audio jeffmccracken Electronic Projects Design/Ideas/Reviews 1 12th March 2003, 07:05 AM
Converting to LED's elfumador General Electronics Chat 0 9th March 2003, 07:16 PM
DIY LED array? Help a newbie. Shang Electronic Projects Design/Ideas/Reviews 7 2nd January 2003, 09:09 AM
Electronic Circuits  |  Learning Electronics

Join our community with over 100,000 Members! It's free, easy and when you're logged in you have many more features! Click to register.
Page Time: 0.04548 seconds      Memory: 7,487 KB      Queries: 16      Templates: 0