User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 456,531 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,890 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 26395 | Replies: 1
Reply
Join Date: Aug 2003
Posts: 1
Reputation: newstar_water is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
newstar_water newstar_water is offline Offline
Newbie Poster

Help Converting byte value into integer and vice versa

  #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.

#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; 
} 

}
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Sep 2003
Posts: 2
Reputation: fzoff is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
fzoff fzoff is offline Offline
Newbie Poster

Re: Converting byte value into integer and vice versa

  #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  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 4:32 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC