Skip to content

Commit

Permalink
[8_11] add binary_to_hexadecimal interface
Browse files Browse the repository at this point in the history
  • Loading branch information
jingkaimori authored Jun 11, 2024
1 parent 62d620e commit 64be652
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 2 deletions.
17 changes: 15 additions & 2 deletions bench/lolly/data/numeral_bench.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@
* \date 2024
*/

#include "file.hpp"
#include "lolly/data/numeral.hpp"
#include <nanobench.h>

using namespace lolly::data;

static ankerl::nanobench::Bench bench;

namespace lolly {
extern void init_tbox ();
} // namespace lolly

int
main () {
lolly::init_tbox ();
ankerl::nanobench::Bench bench;
#ifdef OS_WASM
bench.minEpochIterations (2000);
#else
Expand All @@ -37,4 +37,17 @@ main () {
bench.run ("convert unsigned int to hexadecimal string",
[&] { as_hexadecimal (hex_number, d); });
}
string content= string_load (url_pwd () * "LICENSE");
ankerl::nanobench::Bench bench2;
bench2.relative (true)
.minEpochTime (std::chrono::milliseconds (10))
.run ("convert document to hexadecimal string, legacy",
[&] {
string result;
for (int i= 0; i < N (content); i++)
result << as_hexadecimal ((unsigned char) content[i], 2);
})
.run (
"convert document to hexadecimal string, using binary_to_hexadecimal",
[&] { string result= binary_to_hexadecimal (content); });
}
12 changes: 12 additions & 0 deletions lolly/data/numeral.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -309,5 +309,17 @@ as_hexadecimal (uint32_t i) {
return result;
}

string
binary_to_hexadecimal (string bin) {
string res ((int) (N (bin) * 2));
int cur= 0;
for (unsigned char ch : bin) {
res[cur] = hex_string[ch >> 4];
res[cur + 1]= hex_string[ch & 0x0f];
cur+= 2;
}
return res;
}

} // namespace data
} // namespace lolly
8 changes: 8 additions & 0 deletions lolly/data/numeral.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,5 +100,13 @@ string as_hexadecimal (int i, int length);
*/
string as_hexadecimal (uint32_t i);

/**
* @brief Converts a binary stream to its hexadecimal represention.
*
* @param bin The binary data to be converted to a hexadecimal string.
* @return The hexadecimal string representation of the input binary.
*/
string binary_to_hexadecimal (string bin);

} // namespace data
} // namespace lolly
8 changes: 8 additions & 0 deletions tests/lolly/data/numeral_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,3 +198,11 @@ TEST_CASE ("as_hexadecimal") {
}
SUBCASE ("max") { string_eq (as_hexadecimal (UINT32_MAX), "FFFFFFFF"); }
}

TEST_CASE ("binary to hexadecimal") {
string header_of_png;
header_of_png << "\xff\xd8\xff\xe0";
header_of_png << '\x00';
header_of_png << "\x10\x4A\x46\x49\x46";
string_eq (binary_to_hexadecimal (header_of_png), "FFD8FFE000104A464946");
}

0 comments on commit 64be652

Please sign in to comment.