| | |
Conversion from char to decimal(int)
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
I've read 4 consecutive bytes from a binary file, and store the value as follows,
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.
C++ Syntax (Toggle Plain Text)
int main() { char next4[4] ; while(!fileopen.eof()) { fileopen.read (next4, 4 ) ; } }
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
you could have read it directly into the integer
or you can use the assignment statement
A third way is to use memcpy() to copy the data
C++ Syntax (Toggle Plain Text)
long next4; fileopen.read (&next4, sizeof(long) ) ;
or you can use the assignment statement
C++ Syntax (Toggle Plain Text)
int main() { char next4[4] ; int num; while(!fileopen.eof()) { fileopen.read (next4, 4 ) ; num = *(int *)next4; } }
A third way is to use memcpy() to copy the data
C++ Syntax (Toggle Plain Text)
int num; ... 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.
Thanks,
What is the wrong with this code.
What is the wrong with this code.
C++ Syntax (Toggle Plain Text)
char_traits<char>::char_type ch = next4[4] ; char_traits<char>::int_type int_val ; 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
>>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.
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)
#include <iostream> #include <string> using namespace std ; int main() { char next4[4] = {0}; int n = 123; memcpy(next4, &n, sizeof(int)); char_traits<char>::char_type ch = next4[4] ; char_traits<char>::int_type int_val ; int_val = char_traits<char>::to_int_type (ch) ; cout << "int_val = " << int_val << "\n"; return 0; }
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.
•
•
•
•
>>What is the wrong with this code
Nothing that I know of, other than it doesn't workThis 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
![]() |
Similar Threads
- Conversion from Char* to int ? (C++)
- char+int????? (C++)
- char or int? (C)
- Number System:Conversion from binary to decimal (C)
- Exercise using: unsigned char bcd(int n); (C)
Other Threads in the C++ Forum
- Previous Thread: Skip initial 4 bytes in a binary stream
- Next Thread: Help with pipes (parents reading arrays the child sends through them)
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator getline givemetehcodez graph iamthwee ifstream image input int java lib list loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






