Solved

convert char to int

Posted on 2004-04-12
15
97,247 Views
Last Modified: 2011-08-18
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???
0
Comment
Question by:xenia27
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
  • 2
  • +2
15 Comments
 
LVL 86

Assisted Solution

by:jkr
jkr earned 100 total points
ID: 10810103
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
 
LVL 10

Accepted Solution

by:
Sys_Prog earned 400 total points
ID: 10810794
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
 

Author Comment

by:xenia27
ID: 10810901
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 10

Expert Comment

by:Sys_Prog
ID: 10810910
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
 

Author Comment

by:xenia27
ID: 10810939
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
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 10810964
Let me ask more clearly

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

So, what output do u desire
Amit


0
 

Author Comment

by:xenia27
ID: 10811208
OK...so assume DataBuf[100] = 0x100 and DataBuf[101] = 0x45...so the temperature should be something like "0x6445" or "25669"...^^
0
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 10811289
Hey xenia27

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

Amit
0
 

Author Comment

by:xenia27
ID: 10811308
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
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 10811504
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
 

Author Comment

by:xenia27
ID: 10812058
Thanks a lot...I'm going to try now..^^
0
 
LVL 6

Expert Comment

by:Mafalda
ID: 10812066
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
 
LVL 6

Expert Comment

by:Mafalda
ID: 10812095
It should be:
float temperature = (low_byte + high_byte * 256) / 100.0f; // 116.20 degrees
0
 

Author Comment

by:xenia27
ID: 10812213
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
 
LVL 6

Expert Comment

by:SJT2003A
ID: 11016061
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

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

626 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question