Solved

convert char to int

Posted on 2004-04-12
15
97,235 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
  • 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
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ASP.net build a IF/Then Walkthrough Guide 1 209
How to copy an image file into clipboard C/C++? 1 168
How can i compile this github project?? 2 84
What is sub-make ? 2 60
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

770 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