Thread: Array to int conversion

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    12

    Array to int conversion

    Hello, I have a 10 bit analog to digital binary result storred in an array called:

    Code:
    unsigned char outString[20];
    I would like to convert this to an int to use in an if statement:

    Code:
    if(outString >= 51)
    But when I compile my program (Hi-Tech PICC) I get the error "operands of >= not same type". How would I perform this conversion??

    Basically I have code that takes this 10 bit binary value and ouputs the corresponding int number to my LCD. I would like to take that number and compare it with another number before it prints. My code is the following:

    Code:
    main()
    {
    	int adcvalue, n;
    	unsigned char outString[20];
    	n=0;
    
    	TRISA=0x05;
    
    		lcd_init();
    		init_a2d();
    		LCDclear();
    	
    	while(n==0)
    	{
    			ADGO=1;	
    			while(ADGO)continue;
    			ADIF=0;
    			adcvalue=(ADRESH<< 8) + ADRESL; 
    			sprintf(outString,"&#37;d",adcvalue);
    
    			if(outString >= 51)
    				{			
    					LCDwritestring("98%");
    					ADON=0;
    					n=1;
    				}
          }
    
    	for(;;);
    }

    Thank you.
    Last edited by baseball07; 04-12-2007 at 03:47 PM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    sscanf should suffice. There are also functions such as atoi you can use. For something to do, you could always write your own function to do it.

    If you're working with strings though, you might want to just drop the unsigned qualifier and stick with good ol char.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    12
    I am having trouble understanding what to put into the brackets of the sscanf function, and exactly how it works. Will it just replace the sprintf funtoin? When I compile it still gives me errors.

    Code:
    main()
    {
    	int adcvalue, n, i;
    	unsigned char outString[20];
    	n=0;
    
    	TRISA=0x05;
    
    		lcd_init();
    		init_a2d();
    		LCDclear();
    	
    	while(n==0)
    	{
    			ADGO=1;	
    			while(ADGO)continue;
    			ADIF=0;
    			adcvalue=(ADRESH<< 8) + ADRESL; 
    			sscanf(outString, "&#37;d", adcvalue);
    
    			if(outString >= 51)
    				{			
    					LCDwritestring("98%");
    					ADON=0;
    					n=1;
    				}
          }
    
    	for(;;);
    }

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You pass sscanf the buffer you have your text stored into, and it will scan it for the data matching the format specifiers you're passing it, and store them in the variables you have supplied.
    Code:
    if( sscanf( mystring, "&#37;d", &mynumber ) == 1 )
    {
        printf( "One item scanned, it was %d\n", mynumber );
    }

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    12
    I think I understand what your saying, but I still get errors. "outString" is the buffer I storred my data into, %d, then store result into "int i"? I think I am missing something, I need the int storred in outString and compare it with 51.

    Code:
    sprintf(outString,"%d",adcvalue);
    			sscanf(outString, "%d", &i);
    
    			if( i >= 51)
    				{			
    					LCDwritestring("98%");
    					ADON=0;
    					n=1;
    				}

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What sort of error do you get? Also, if LCDwritestring uses any of the standard printf like functions, the &#37; sign will be interpreted as the start of a conversion sequence.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    If you've already got the value as an integer, and then also create a string representation of this value, why would you need to convert the string representation of the integer that you just converted back into an integer value?

    Would something like this work?
    Code:
    int main(void)
    {
       unsigned int adcvalue;
    
       TRISA = 0x05;
       lcd_init();
       init_a2d();
       LCDclear();
    
       for ( ;; )
       {
          char text[20];
    
          ADGO = 1;  
          while ( ADGO )
          {
             /* wait for conversion */
          }
          ADIF = 0;
          adcvalue = (ADRESH << 8) + ADRESL; /* can't do 'adcvalue = ADRES;' ? */
          sprintf(text, "%u", adcvalue);
          LCDwritestring(text);
          if ( adcvalue >= 51 )
          {
             LCDwritestring("98%");
             ADON = 0; /* turn off A/D */
             break; /* bail out of the loop and exit main */
          }
       }
       return 0;
    }
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. Need help understanding info in a header file
    By hicpics in forum C Programming
    Replies: 8
    Last Post: 12-02-2005, 12:36 PM
  3. Switch/case Problems (long code in post)
    By Wraithan in forum C++ Programming
    Replies: 2
    Last Post: 12-01-2005, 06:40 PM
  4. problem with sorting
    By pinkpenguin in forum C Programming
    Replies: 2
    Last Post: 11-18-2005, 11:06 AM
  5. getting a headache
    By sreetvert83 in forum C++ Programming
    Replies: 41
    Last Post: 09-30-2005, 05:20 AM