Skip to content

Commit 64be652

Browse files
authored
[8_11] add binary_to_hexadecimal interface
1 parent 62d620e commit 64be652

File tree

4 files changed

+43
-2
lines changed

4 files changed

+43
-2
lines changed

bench/lolly/data/numeral_bench.cpp

+15-2
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,20 @@
55
* \date 2024
66
*/
77

8+
#include "file.hpp"
89
#include "lolly/data/numeral.hpp"
910
#include <nanobench.h>
1011

1112
using namespace lolly::data;
1213

13-
static ankerl::nanobench::Bench bench;
14-
1514
namespace lolly {
1615
extern void init_tbox ();
1716
} // namespace lolly
1817

1918
int
2019
main () {
2120
lolly::init_tbox ();
21+
ankerl::nanobench::Bench bench;
2222
#ifdef OS_WASM
2323
bench.minEpochIterations (2000);
2424
#else
@@ -37,4 +37,17 @@ main () {
3737
bench.run ("convert unsigned int to hexadecimal string",
3838
[&] { as_hexadecimal (hex_number, d); });
3939
}
40+
string content= string_load (url_pwd () * "LICENSE");
41+
ankerl::nanobench::Bench bench2;
42+
bench2.relative (true)
43+
.minEpochTime (std::chrono::milliseconds (10))
44+
.run ("convert document to hexadecimal string, legacy",
45+
[&] {
46+
string result;
47+
for (int i= 0; i < N (content); i++)
48+
result << as_hexadecimal ((unsigned char) content[i], 2);
49+
})
50+
.run (
51+
"convert document to hexadecimal string, using binary_to_hexadecimal",
52+
[&] { string result= binary_to_hexadecimal (content); });
4053
}

lolly/data/numeral.cpp

+12
Original file line numberDiff line numberDiff line change
@@ -309,5 +309,17 @@ as_hexadecimal (uint32_t i) {
309309
return result;
310310
}
311311

312+
string
313+
binary_to_hexadecimal (string bin) {
314+
string res ((int) (N (bin) * 2));
315+
int cur= 0;
316+
for (unsigned char ch : bin) {
317+
res[cur] = hex_string[ch >> 4];
318+
res[cur + 1]= hex_string[ch & 0x0f];
319+
cur+= 2;
320+
}
321+
return res;
322+
}
323+
312324
} // namespace data
313325
} // namespace lolly

lolly/data/numeral.hpp

+8
Original file line numberDiff line numberDiff line change
@@ -100,5 +100,13 @@ string as_hexadecimal (int i, int length);
100100
*/
101101
string as_hexadecimal (uint32_t i);
102102

103+
/**
104+
* @brief Converts a binary stream to its hexadecimal represention.
105+
*
106+
* @param bin The binary data to be converted to a hexadecimal string.
107+
* @return The hexadecimal string representation of the input binary.
108+
*/
109+
string binary_to_hexadecimal (string bin);
110+
103111
} // namespace data
104112
} // namespace lolly

tests/lolly/data/numeral_test.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -198,3 +198,11 @@ TEST_CASE ("as_hexadecimal") {
198198
}
199199
SUBCASE ("max") { string_eq (as_hexadecimal (UINT32_MAX), "FFFFFFFF"); }
200200
}
201+
202+
TEST_CASE ("binary to hexadecimal") {
203+
string header_of_png;
204+
header_of_png << "\xff\xd8\xff\xe0";
205+
header_of_png << '\x00';
206+
header_of_png << "\x10\x4A\x46\x49\x46";
207+
string_eq (binary_to_hexadecimal (header_of_png), "FFD8FFE000104A464946");
208+
}

0 commit comments

Comments
 (0)