-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhexadecimal.cpp
More file actions
131 lines (114 loc) · 3.48 KB
/
hexadecimal.cpp
File metadata and controls
131 lines (114 loc) · 3.48 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include "hexadecimal.h"
Hexadecimal::Hexadecimal()
{
this->pathOfInputFile = " ";
this->pathOfOutputFile = " ";
}
Hexadecimal::Hexadecimal(string pathOfInputFile, string pathOfOutputFile){
this->pathOfInputFile = pathOfInputFile;
this->pathOfOutputFile = pathOfOutputFile;
}
void Hexadecimal::setPathOfInputFile(string pathOfInputFile)
{
this->pathOfInputFile = pathOfInputFile;
}
void Hexadecimal::setPathOfOutputFile(string pathOfOutputFile)
{
this->pathOfOutputFile = pathOfOutputFile;
}
void Hexadecimal::deleteHexaFile(char pathOfHexaFile[])
{
remove(pathOfHexaFile);
}
/*------------------File TO Hexa Class Code -----------------*/
FileToHexa::FileToHexa() : Hexadecimal(){}
FileToHexa::FileToHexa(string pathOfInputFile, string pathOfOutputFile) : Hexadecimal(pathOfInputFile, pathOfOutputFile){}
bool FileToHexa::convertfile()
{
vector<unsigned char> file_data;
bool flag = true;
fstream inputFile;
inputFile.open(pathOfInputFile, ios::in | ios::binary);
if(!inputFile.is_open())
{
system("cls");
system("Color 04");
cout<<"\n\n\t\t\t\tUnable to open a "<<pathOfInputFile<<" file :( "<<endl;
system("pause");
flag = false;
return false;
}else{
while (true) {
unsigned char byte;
inputFile.read(reinterpret_cast<char*>(&byte), sizeof(byte));
if (inputFile.eof()) {
break;
}
file_data.push_back(byte);
}
inputFile.close();
}
if(flag){
fstream outputFile;
outputFile.open(pathOfOutputFile, ios::out | ios::binary);
if(!outputFile.is_open())
{
system("cls");
system("Color 04");
cout<<"\n\n\t\t\t\tUnable to open a "<<pathOfOutputFile<<" file :( "<<endl;
system("pause");
return false;
}else{
for (unsigned char byte : file_data) {
outputFile << hex << setw(2) << setfill('0') << static_cast<int>(byte) << ' ';
}
}
outputFile.close();
return true;
}
}
/*------------------Hexa To File Class Code -----------------*/
HexaToFile::HexaToFile(): Hexadecimal(){}
HexaToFile::HexaToFile(string pathOfInputFile, string pathOfOutputFile): Hexadecimal(pathOfInputFile, pathOfOutputFile){}
bool HexaToFile::convertfile()
{
vector <unsigned char> file_data;
bool flag = true;
fstream inputFile;
inputFile.open(pathOfInputFile, ios::in | ios::binary);
if(!inputFile.is_open())
{
system("cls");
system("Color 04");
cout<<"\n\n\t\t\t\tUnable to open a "<<pathOfInputFile<<" file :( "<<endl;
system("pause");
flag = false;
return false;
}else{
string value;
while(inputFile >> value)
{
unsigned char byte;
byte = static_cast<unsigned char>(stoi(value, nullptr, 16));
file_data.push_back(byte);
}
inputFile.close();
}
if(flag){
fstream outputFile;
outputFile.open(pathOfOutputFile, ios::out | ios::binary);
if(!outputFile.is_open())
{
system("cls");
system("Color 04");
cout<<"\n\n\t\t\t\tUnable to open a "<<pathOfOutputFile<<" file :( "<<endl;
system("pause");
return false;
}else{
char *ch = reinterpret_cast<char*>(&file_data[0]);
outputFile.write(ch, file_data.size());
outputFile.close();
return true;
}
}
}