Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8_11] add binary_to_hexadecimal interface #335

Merged
merged 2 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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++)
da-liii marked this conversation as resolved.
Show resolved Hide resolved
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) {
da-liii marked this conversation as resolved.
Show resolved Hide resolved
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");
}
Loading