c++ large binary to decimal conversion

hi this code print the benary value(01000011...) of any file, i wante to convert each 3 bits like 011 to its decimal value, please help me

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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
 #include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <vector>
#include <bitset>
#include <stdio.h>
using namespace std;
int main(int argc, char** argv)
{

	int achunk = 100;
	// get length of file:



	ifstream bigfile;
	bigfile.open("text.txt", ios::in | ios::binary);
	bigfile.seekg(0, ios::end);
	int length = bigfile.tellg();
	bigfile.seekg(0, bigfile.beg);

	int moddulo = length%achunk;
	int parts = length / achunk;
	int postion = parts*achunk;
	int counter = 0;
	//vector<char> buffer (achunk);



	for (int i = 0; i < parts; i++)
	{




		bigfile.seekg(achunk*i, ios::end);
		bigfile.seekg(achunk*i, bigfile.beg);

		char * buffer = new char[achunk];
		bigfile.read(buffer, achunk);



		/*char buffer2[100];*/

		/*strncpy(buffer2, buffer, 100);*/

		cout << "part No  " <<i;
		cout << " contain binary value:";

		for (int j = 0; j <100; ++j)
		{
			 cout << bitset<8>(buffer[j]) << " ";
		 //cout << (int)(buffer[j]) << " ";





		}
		cout << endl << endl;


		/*cout << buffer << endl;
		delete[] buffer;*/
		//cout << moddulo << endl;
		//cout << postion<< endl;


	}
	if (!length%achunk == 0)
	{

		/*ifstream bigfile;
		bigfile.open("text.txt", ios::in | ios::binary);*/


		bigfile.seekg(postion, bigfile.beg);

		char * buffer = new char[moddulo];

		bigfile.read(buffer, moddulo);

		cout << "last part contain binary value  :";
		for (int j = 0; j <moddulo; ++j)
		{
			cout << bitset<8>(buffer[j])<<" ";

		}
		//cout << "binary value of the last  part  <<   << endl;

		delete[] buffer;
		bigfile.close();


		// cout << moddulo << endl;
		//cout << postion<< endl;

	}

	system("pause");

	return 0;

}







What of files that don't contain an amount of bits that is evenly divisible by 3?
Hi thank you for your replay, how to do this?
Hi thank you for your replay, how to do this?


You already have this :
int length = bigfile.tellg();

You only need this check to check if the file contains an amount of bits that is evenly divisible by 3 :
if(length % 3 == 0)
Topic archived. No new replies allowed.