How do I work with binary and individual bits and write just one byte?

I am a little frustrated because all the books I have are working in integers or other that are at least four bytes each. I want to be able to read in a variable number of bytes in binary and alter them as I see fit then write them back.
How would I do this?
Thanks
Last edited on
You do this with bit shifting, e.g.
uint val=byte1 | (byte2<<8) | (byte3<<16);
A char is guaranteed to be one byte. If you read in a char, you've read one byte.
A char is one byte of data that is stored as a integer. Just make sure you cast them as a int before you print them out like so:

1
2
3
4
5
6
7
8
//C++ version.
#include <iostream>
using namespace std;

...

char byte = 42;
cout << (int)byte;


or

1
2
3
4
5
6
7
8
//C version.
//no actual casting needed here.
#include <stdio.h>

...

char byte = 42;
printf("%d", byte);


If this is not what you meant, I am sorry. You need to explain yourself a little more.
but if I cast it as an int wont that convert it to four bytes instead of one?
Why cast to an int? Just read a byte, play with it, write it, repeat.
I don't want to cast to an int I want to read in a set of bytes, play with them(as you said), and output to a file when the buffer of 4096 is reached.
4096 bytes that is. Which is faster at run time using binary or chars?
I think chars is fastest (depending on what you are doing) because to play with binary you generally have to do some shifting around. If you're not doing any shifting and using bitwise ORs (|), bitwise ands (&) or bitwise XORs (^), then binary is probably easier/faster because there are fewer operations.

I think a 32 bit processor will generally read memory in 32 bit (4 byte) blocks anyways so if you are trying to save time by using chars, I don't think this will help.

If you are trying to save space, then just writing everything as chars when you save to a file. You don't need to cast then as ints. However, when you read the file, just ensure that you import them as single bytes.
Back to your original question:

If you wanted to read in a variable number of bytes you'd have to have some way of knowing how many bytes are in each word. I don't know if this method would work. you may get some compiling errors, but for example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
unsigned long long input;
int numBytes; //You need to set this somehow
ifstream fin("myfile.txt")
fin >> input;

switch numBytes
{
case 1:
  cout << (unsigned char)(input & 0xff);
case 2:
  cout << (unsigned short)(input & 0xffff);
case 4:
  cout << (unsigned int)(input & 0xffffffff);
case 8:
  cout << (unsigned long long)(input & 0xffffffffffffffff);
}


If you really just don't want to use more than 1 byte, then simply don't use anything but chars or bools in your code.
Last edited on
istream::read() works with chars;
http://www.cplusplus.com/reference/iostream/istream/read/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// get file size, or process it in blocks
size_t bufferSize = ...
size_t bytesRead = 0;

std::vector<char> buffer(bufferSize);

ifstream fin("myfile.txt")
if(fin.is_open())
{
    fin.read(&buffer[0], bufferSize);
    size_t bytesRead = fin.gcount(); // more error handling should be added!

    // process bytes
    for(size_t idx = 0; idx < bytesRead; ++idx)
    {
        buffer[idx] = toupper(buffer[idx]); // or some more useful manipulation
    }

    fin.close();
}

ofstream fout("myoutput.txt")
if(fout.is_open())
{
    fout.write(&buffer[0], bytesRead);
    fout.close();
}


Andy
Last edited on
Topic archived. No new replies allowed.