convert char to int

Hi,

I wanna know how to convert a string to int.

char DataBuf[200];
int   Temperature;
char TempStr[2];

TempStr[0] = DataBuf[100];
TempStr[1] = DataBuf[101];

How can I get Temperature from TempStr???
xenia27Asked:
Who is Participating?
 
Sys_ProgConnect With a Mentor Commented:
One more way to convert string to numeric values


#include <iostream>
#include <string>
#include <sstream>

using namespace std ;

int main(int argc, char* argv[])
{
      string str ( "123.5 1526" ) ;
      istringstream s ( str ) ;
      int i ;
      float f ;
      s >> f ;
      s >> i ;
      cout << f ;
      cout << i ;
    system("pause");
      return 0;
}


Amit
0
 
jkrConnect With a Mentor Commented:
That will not work like that at all.You'd need to use a library fuction like 'strtol()' or 'atoi()', e.g

/* ATOF.C: This program shows how numbers stored
 * as strings can be converted to numeric values
 * using the atof, atoi, and atol functions.
 */

#include <stdlib.h>
#include <stdio.h>

void main( void )
{
   char *s; double x; int i; long l;

   s = "  -2309.12E-15";    /* Test of atof */
   x = atof( s );
   printf( "atof test: ASCII string: %s\tfloat:  %e\n", s, x );

   s = "7.8912654773d210";  /* Test of atof */
   x = atof( s );
   printf( "atof test: ASCII string: %s\tfloat:  %e\n", s, x );

   s = "  -9885 pigs";      /* Test of atoi */
   i = atoi( s );
   printf( "atoi test: ASCII string: %s\t\tinteger: %d\n", s, i );

   s = "98854 dollars";     /* Test of atol */
   l = atol( s );
   printf( "atol test: ASCII string: %s\t\tlong: %ld\n", s, l );
}


/* STRTOD.C: This program uses strtod to convert a
 * string to a double-precision value; strtol to
 * convert a string to long integer values; and strtoul
 * to convert a string to unsigned long-integer values.
 */

#include <stdlib.h>
#include <stdio.h>

void main( void )
{
   char   *string, *stopstring;
   double x;
   long   l;
   int    base;
   unsigned long ul;
   string = "3.1415926This stopped it";
   x = strtod( string, &stopstring );
   printf( "string = %s\n", string );
   printf("   strtod = %f\n", x );
   printf("   Stopped scan at: %s\n\n", stopstring );
   string = "-10110134932This stopped it";
   l = strtol( string, &stopstring, 10 );
   printf( "string = %s", string );
   printf("   strtol = %ld", l );
   printf("   Stopped scan at: %s", stopstring );
   string = "10110134932";
   printf( "string = %s\n", string );
   /* Convert string using base 2, 4, and 8: */
   for( base = 2; base <= 8; base *= 2 )
   {
      /* Convert the string: */
      ul = strtoul( string, &stopstring, base );
      printf( "   strtol = %ld (base %d)\n", ul, base );
      printf( "   Stopped scan at: %s\n", stopstring );
   }


The rest depends on your about needs what that program should do...
0
 
xenia27Author Commented:
I try to use "atoi" but the answer I got seems not right.

This is what I did...I have a page buffer which contains all different data.
And Temperature takes two bytes in that page buffer..DataBuf[100] and DataBuf[101]

How can I read from DataBuf and get a numeric value???
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
Sys_ProgCommented:
xenia27,

What kind of values are present in DataBuf[100] and DataBuf[101]
Could u give an example of what u actually have & what u require

Amit
0
 
xenia27Author Commented:
all values in DataBuf are hex values and DataBuf[100] and DataBuf[101] together will be the value of a temperature.  And the temperature value I want is in either hex or dec value.

I guess I should ask how can I put these two bytes together so I can get one number??
0
 
Sys_ProgCommented:
Let me ask more clearly

Lets say
u have
DataBuf[100] = 100 ;
DataBuf[101] = 45 ;

So, what output do u desire
Amit


0
 
xenia27Author Commented:
OK...so assume DataBuf[100] = 0x100 and DataBuf[101] = 0x45...so the temperature should be something like "0x6445" or "25669"...^^
0
 
Sys_ProgCommented:
Hey xenia27

I am really confused about how did u arrive at that figure
could u elaborate

Amit
0
 
xenia27Author Commented:
sorry for making you confused...@@

DataBuf[100] = 100
DataBuf[101] = 45   <-- are these two values hex values??

I thought they are hex value so I turn them into decimal values.

Anyway, if they are not hex values...I just want the temperature value equals to "10045".
0
 
Sys_ProgCommented:
First of all, u cannot have
DataBuf[100] = 100

since DataBuf is a character array, each location of DataBuf would contain a character & 100 is conmposed of 3 characetsr - 1,0,0

Hence, I assume y mean to convert a char string to a int

Have a look at this program which converts a char string to an int

#include <iostream>

using namespace std ;

int main(int argc, char* argv[])
{
      char str[] = "17445" ;

      int val = 0 ;
      int len ;
      len = strlen ( str ) ;
      for ( int i = 0 ; i < len ; i ++ ) {
            val = ( val * 10 ) + ( str [i] - '0' ) ;

      }
        cout << val ;
     system("pause");
      return 0;
}



Amit
0
 
xenia27Author Commented:
Thanks a lot...I'm going to try now..^^
0
 
MafaldaCommented:
I have several cases where the two bytes represent the Temp in the following way:

unsigned char low_byte = DataBuf[100];  // 100 in your case -> can't be hex ... 0x100 is greater than the maximum 0xff
unsigned char high_byte = DataBuf[101];  // 45 in your case

float temperature = (low_byte + high_byte * 256) / 100.0f = 116.20 degrees


Maybe it is 45 and 00 and the order is high-low yielding 45  0r 0x45

It is interesting wher these numbers are comming from ;o)
0
 
MafaldaCommented:
It should be:
float temperature = (low_byte + high_byte * 256) / 100.0f; // 116.20 degrees
0
 
xenia27Author Commented:
Thanks~  I got my answer...^^
But I cannot give you any point now..but if you want...I can post another post to give you some point.
0
 
SJT2003ACommented:
There is another simple way, in C. Check out the following code to convert a number from string form to numeric form.

#include <stdio.h>
void main()
{
   char str[]="1234.45";
   float num;

   sscanf(str,"%f",&num);

   printf("\n the converted number is %4.2f ",num);
   return;
}


This prints out the value 1234.45 as converted. If it is sure that it holds a numeric integer, then the sscanf() can be used with %d format.

If it is needed to be done in C++, can be done using istringstream buffer as already posted by someone.

Hope this helps. Good luck :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.