943,467 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 64331
  • C++ RSS
Aug 25th, 2003
1

Converting byte value into integer and vice versa

Expand Post »
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.

C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 11
Solved Threads: 0
Newbie Poster
newstar_water is offline Offline
1 posts
since Aug 2003
Sep 2nd, 2003
1

Re: Converting byte value into integer and vice versa

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

}
Reputation Points: 11
Solved Threads: 0
Newbie Poster
fzoff is offline Offline
2 posts
since Sep 2003
Jul 2nd, 2010
-2

convert string byte to actual byte value

for ex.
string s = "0x31"

i want to convert the string into a byte value...say store in char c.
so i print c...it would display "1".
How to do this?
Last edited by iadman; Jul 2nd, 2010 at 12:09 am.
Reputation Points: 9
Solved Threads: 0
Newbie Poster
iadman is offline Offline
2 posts
since Jul 2010
Jul 2nd, 2010
0
Re: Converting byte value into integer and vice versa
Hello,

I need to know how the get the integer value of a byte and vice versa.
As the first respondent answered in unformatted code, it's as simple as a cast. I would additionally point out that it is useful to understand the type system in c++.

There are several integer types. These include

char
unsigned char
short
unsigned short
int
unsigned int
long
unsigned long

All of these types may be cast between each other directly.
Reputation Points: 152
Solved Threads: 41
Posting Whiz in Training
dusktreader is offline Offline
255 posts
since Jan 2010

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: sort() for double linked list
Next Thread in C++ Forum Timeline: How to save a string input as a double linked list in dynamic memory in C++





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


Follow us on Twitter


© 2011 DaniWeb® LLC