| | |
Converting byte value into integer and vice versa
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Aug 2003
Posts: 1
Reputation:
Solved Threads: 0
Hello,
I need to know how the get the integer value of a byte and vice versa.
I want to get the integer value from a byte read from a binary file.
I also want to convert the integer value back into a byte value for write.
Below I have code where the current byte is read into the variable 'byte". I am basically copying one file to the other.
Thanks.
I need to know how the get the integer value of a byte and vice versa.
I want to get the integer value from a byte read from a binary file.
I also want to convert the integer value back into a byte value for write.
Below I have code where the current byte is read into the variable 'byte". I am basically copying one file to the other.
Thanks.
C++ Syntax (Toggle Plain Text)
#include <fstream.h> #include <iomanip.h> int main() { int ind, tot; unsigned char byte; ifstream infile("inputfile.dat", ios::in | ios::binary); ofstream outfile("outputfile.dat", ios::binary ); for (!infile) { infile.read( byte, 1 ); outfile.write( byte, 1 ); return 0; } }
•
•
Join Date: Sep 2003
Posts: 2
Reputation:
Solved Threads: 0
#include <fstream.h>
#include <iomanip.h>
int main()
{
int ind, tot;
unsigned char byte;
ifstream infile("inputfile.dat", ios::in | ios::binary);
ofstream outfile("outputfile.dat", ios::binary );
for (!infile)
{
int iVal;
unsigned char ucVal;
infile.read( byte, 1 );
//this code I test with Visual C++.
//convert unsigned char to integer
iVal = (int)byte; //int cast
//convert integer value to unsigned char
ucVal = (unsigned char)iVal; //cast to unsigned char
outfile.write( byte, 1 );
return 0;
}
}
#include <iomanip.h>
int main()
{
int ind, tot;
unsigned char byte;
ifstream infile("inputfile.dat", ios::in | ios::binary);
ofstream outfile("outputfile.dat", ios::binary );
for (!infile)
{
int iVal;
unsigned char ucVal;
infile.read( byte, 1 );
//this code I test with Visual C++.
//convert unsigned char to integer
iVal = (int)byte; //int cast
//convert integer value to unsigned char
ucVal = (unsigned char)iVal; //cast to unsigned char
outfile.write( byte, 1 );
return 0;
}
}
![]() |
Similar Threads
- Code Snippet: Converting a numerical value to a string and vice-versa (C++)
- Code Snippet: Converting Fahrenheit-Celsius & Vice-Versa (C)
- Convert a word in kanji to katakana or vice-versa. (VB.NET)
- How to Insert Picture to SQL Database (VB.NET)
- 2D array to 1D array vice versa?? (VB.NET)
- Convert double from Small Endian to Big Endian and vice versa (C#)
- convert lower case letters to uppercase and vice-versa (C++)
- 1 .Exporting data from Dataset to XLl sheet in VB.net Windows Application &vice versa (VB.NET)
Other Threads in the C++ Forum
- Previous Thread: New To Programming = ME!!
- Next Thread: struct type redefinition
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll dynamiccharacterarray email encryption error file format forms fstream function functions game givemetehcodez graph gui homeworkhelp iamthwee ifstream input int java lib library lines list loop looping loops map math matrix memory newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sorting spoonfeeding string strings struct temperature template templates text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets





