-
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,"%d",adcvalue);
if(outString >= 51)
{
LCDwritestring("98%");
ADON=0;
n=1;
}
}
for(;;);
}
Thank you.
-
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.
-
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, "%d", adcvalue);
if(outString >= 51)
{
LCDwritestring("98%");
ADON=0;
n=1;
}
}
for(;;);
}
-
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, "%d", &mynumber ) == 1 )
{
printf( "One item scanned, it was %d\n", mynumber );
}
Quzah.
-
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;
}
-
What sort of error do you get? Also, if LCDwritestring uses any of the standard printf like functions, the % sign will be interpreted as the start of a conversion sequence.
Quzah.
-
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;
}