We help IT Professionals succeed at work.

convert char to int

xenia27
xenia27 asked
on
97,398 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???
Comment
Watch Question

jkr
CERTIFIED EXPERT
Top Expert 2012
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

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???

Commented:
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

Author

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??

Commented:
Let me ask more clearly

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

So, what output do u desire
Amit


Author

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

Commented:
Hey xenia27

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

Amit

Author

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".

Commented:
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

Author

Commented:
Thanks a lot...I'm going to try now..^^

Commented:
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)

Commented:
It should be:
float temperature = (low_byte + high_byte * 256) / 100.0f; // 116.20 degrees

Author

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.

Commented:
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 :)
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.