Conversion from char to decimal(int)

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2007
Posts: 185
Reputation: eranga262154 is an unknown quantity at this point 
Solved Threads: 2
eranga262154's Avatar
eranga262154 eranga262154 is offline Offline
Junior Poster

Conversion from char to decimal(int)

 
0
  #1
Sep 18th, 2007
I've read 4 consecutive bytes from a binary file, and store the value as follows,

  1. int main()
  2. {
  3. char next4[4] ;
  4.  
  5. while(!fileopen.eof())
  6. {
  7. fileopen.read (next4, 4 ) ;
  8. }
  9.  
  10. }

What I want to do is, that value in next4 want to convert into a decimal/int value. How can I do that. Search on MSDN and unable to find a clear explanation.
“victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,585
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1487
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Conversion from char to decimal(int)

 
0
  #2
Sep 18th, 2007
you could have read it directly into the integer
  1. long next4;
  2. fileopen.read (&next4, sizeof(long) ) ;

or you can use the assignment statement
  1. int main()
  2. {
  3. char next4[4] ;
  4. int num;
  5. while(!fileopen.eof())
  6. {
  7. fileopen.read (next4, 4 ) ;
  8. num = *(int *)next4;
  9. }
  10.  
  11. }

A third way is to use memcpy() to copy the data
  1. int num;
  2. ...
  3. memcpy(&num, next4, sizeof(long));
Last edited by Ancient Dragon; Sep 18th, 2007 at 8:35 am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 185
Reputation: eranga262154 is an unknown quantity at this point 
Solved Threads: 2
eranga262154's Avatar
eranga262154 eranga262154 is offline Offline
Junior Poster

Re: Conversion from char to decimal(int)

 
0
  #3
Sep 18th, 2007
Thanks,

What is the wrong with this code.


  1. char_traits<char>::char_type ch = next4[4] ;
  2. char_traits<char>::int_type int_val ;
  3. int_val = char_traits<char>::to_int_type (ch) ;
“victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,585
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1487
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Conversion from char to decimal(int)

 
0
  #4
Sep 18th, 2007
>>What is the wrong with this code
Nothing that I know of, other than it doesn't work This produces the wrong value, probably because line 10 is only one byte of a 4-byte number.
  1. #include <iostream>
  2. #include <string>
  3. using namespace std ;
  4.  
  5. int main()
  6. {
  7. char next4[4] = {0};
  8. int n = 123;
  9. memcpy(next4, &n, sizeof(int));
  10. char_traits<char>::char_type ch = next4[4] ;
  11. char_traits<char>::int_type int_val ;
  12. int_val = char_traits<char>::to_int_type (ch) ;
  13.  
  14. cout << "int_val = " << int_val << "\n";
  15. return 0;
  16.  
  17. }
Last edited by Ancient Dragon; Sep 18th, 2007 at 8:55 am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 185
Reputation: eranga262154 is an unknown quantity at this point 
Solved Threads: 2
eranga262154's Avatar
eranga262154 eranga262154 is offline Offline
Junior Poster

Re: Conversion from char to decimal(int)

 
0
  #5
Sep 19th, 2007
Originally Posted by Ancient Dragon View Post
>>What is the wrong with this code
Nothing that I know of, other than it doesn't work This produces the wrong value, probably because line 10 is only one byte of a 4-byte number.

Thanks, I got the point and stuck with this whole night. When I use it only on byte count there in my code.

Thanks again,
“victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC