-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbin-to-hex.cpp
105 lines (75 loc) · 2.37 KB
/
bin-to-hex.cpp
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
#include <iostream>
#include <fstream>
#include <filesystem>
#include <sstream>
#pragma warning(disable:6385)
int main(int argc, char** argv)
{
// check for valid argument count
if (argc != 3) {
std::cout << "error: invalid argument count" << std::endl;
return -1;
}
// check if source file exists
if (!std::filesystem::exists(argv[1])) {
std::cout << "error: source file does not exist" << std::endl;
return -2;
}
// read file into process
char* source;
std::streamsize source_size;
try
{
std::ifstream read_stream(argv[1], std::ios::binary);
if (!read_stream.is_open()) {
std::cout << "error: failed to open source file" << std::endl;
return -6;
}
read_stream.ignore(std::numeric_limits<std::streamsize>::max());
source_size = read_stream.gcount();
read_stream.clear();
source = new char[static_cast<size_t>(source_size)];
read_stream.seekg(0, std::ios::beg);
read_stream.read(source, source_size);
read_stream.close();
}
catch (...)
{
std::cout << "error: failed to read source file" << std::endl;
return -3;
}
// convert file to hex
std::stringstream output{};
output << "#pragma once\n\nnamespace binary {\n\tunsigned char buffer[] = {";
constexpr size_t line_max = 24;
for (auto i = 0; i < source_size; i++)
{
if (i % line_max == 0)
output << "\n\t\t";
output << "0x" << std::hex << std::setw(2) << std::setfill('0') << static_cast<uint32_t>(static_cast<uint8_t>(source[i])) << std::dec << ", ";
}
output << "\n\t};\n}";
// delete source buffer
delete[] source;
// write file
try
{
std::ofstream write_stream(argv[2], std::ios::out | std::ios::binary | std::ios::trunc);
if (!write_stream.is_open()) {
std::cout << "error: failed to open result file" << std::endl;
return -4;
}
write_stream.write(output.str().c_str(), output.str().length());
write_stream.close();
}
catch (...)
{
std::cout << "error: failed to write result file" << std::endl;
return -5;
}
// free output memory
output.clear();
// success
std::cout << "success: file dumped to header file" << std::endl;
return 0;
}