944,085 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 5243
  • C++ RSS
Sep 18th, 2007
0

Conversion from char to decimal(int)

Expand Post »
I've read 4 consecutive bytes from a binary file, and store the value as follows,

C++ Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 32
Solved Threads: 2
Junior Poster
eranga262154 is offline Offline
185 posts
since Sep 2007
Sep 18th, 2007
0

Re: Conversion from char to decimal(int)

you could have read it directly into the integer
C++ Syntax (Toggle Plain Text)
  1. long next4;
  2. fileopen.read (&next4, sizeof(long) ) ;

or you can use the assignment statement
C++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  1. int num;
  2. ...
  3. memcpy(&num, next4, sizeof(long));
Last edited by Ancient Dragon; Sep 18th, 2007 at 8:35 am.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,957 posts
since Aug 2005
Sep 18th, 2007
0

Re: Conversion from char to decimal(int)

Thanks,

What is the wrong with this code.


C++ Syntax (Toggle Plain Text)
  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) ;
Reputation Points: 32
Solved Threads: 2
Junior Poster
eranga262154 is offline Offline
185 posts
since Sep 2007
Sep 18th, 2007
0

Re: Conversion from char to decimal(int)

>>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.
C++ Syntax (Toggle Plain Text)
  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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,957 posts
since Aug 2005
Sep 19th, 2007
0

Re: Conversion from char to decimal(int)

>>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,
Reputation Points: 32
Solved Threads: 2
Junior Poster
eranga262154 is offline Offline
185 posts
since Sep 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Skip initial 4 bytes in a binary stream
Next Thread in C++ Forum Timeline: Help with pipes (parents reading arrays the child sends through them)





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC