Solved

convert char to int

Posted on 2004-04-12
15
97,227 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
 
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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 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…

708 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now