On Thursday, 5 April 2018 at 08:57:11 UTC, SimonN wrote:
On Thursday, 5 April 2018 at 08:12:38 UTC, aerto wrote:
This is the bitcoin genesis block merkle root
4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b how i can get it at this format 3ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4a ??
i try it using
string merkle =
"4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b";
writeln(merkle.retro.text); and it gives me
b33adedfa7b7212ba77cc2e37667f816f78cb13c88a81523a3f98baab4e1e5a4
Here's one solution with std.range.chunks. A small downside is
that it needs the array allocation in the middle because chunks
cannot offer the bi-directional range necessary for retro.
import std.range;
import std.algorithm;
void main()
{
string merkle =
"4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b";
assert (merkle.retro.equal(
"b33adedfa7b7212ba77cc2e37667f816f78cb13c88a81523a3f98baab4e1e5a4"));
assert (merkle.chunks(2).array.retro.joiner.equal(
"3ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4a"));
}
-- Simon
FYI: The problem isn't chunks, but that strings aren't
bi-directional ranges (hello ugly auto-decoding!).
Simply disable it with byCodeUnit:
---
import std.experimental.all;
void main()
{
"4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b".byCodeUnit.chunks(2).retro.joiner.writeln;
"4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b".byCodeUnit.slide(2, 2).retro.joiner.writeln;
}
---
Oh and the new slide is a generalization of chunks.
https://run.dlang.io/is/ggrh14