Converting byte value into integer and vice versa

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

Join Date: Aug 2003
Posts: 1
Reputation: newstar_water is an unknown quantity at this point 
Solved Threads: 0
newstar_water newstar_water is offline Offline
Newbie Poster

Converting byte value into integer and vice versa

 
1
  #1
Aug 25th, 2003
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.

  1. #include <fstream.h>
  2. #include <iomanip.h>
  3. int main()
  4. {
  5. int ind, tot;
  6. unsigned char byte;
  7. ifstream infile("inputfile.dat", ios::in | ios::binary);
  8. ofstream outfile("outputfile.dat", ios::binary );
  9.  
  10. for (!infile)
  11. {
  12. infile.read( byte, 1 );
  13. outfile.write( byte, 1 );
  14. return 0;
  15. }
  16.  
  17. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2003
Posts: 2
Reputation: fzoff is an unknown quantity at this point 
Solved Threads: 0
fzoff fzoff is offline Offline
Newbie Poster

Re: Converting byte value into integer and vice versa

 
1
  #2
Sep 2nd, 2003
#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;
}

}
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