There must be a better way to do this! Thanks!
// 0 - we update the epoch
if (count == 0) {
let dig = SHA256.Digest();
dig.write(epoch);
// Time bytes
let time : Nat64 = Nat64.fromIntWrap(Time.now());
let t1 : Nat8 = Nat8.fromNat(Nat64.toNat((time << 0) & 255));
let t2 : Nat8 = Nat8.fromNat(Nat64.toNat((time << 8) & 255));
let t3 : Nat8 = Nat8.fromNat(Nat64.toNat((time << 16) & 255));
let t4 : Nat8 = Nat8.fromNat(Nat64.toNat((time << 24) & 255));
dig.write([t1, t2, t3, t4]);
epoch := dig.sum();
};
2 Likes
// Time bytes
let time : Nat64 = Nat64.fromIntWrap(Time.now());
let buf = Buffer.Buffer<Nat8>(8);
for (i in Iter.range(0, 7)) {
let b = Nat64.fromNat(i*8);
let n = Nat8.fromNat(Nat64.toNat((time << b) & 255));
buf.add(n);
};
dig.write(buf.toArray());
slightly better, but all the conversions are crazy and took me a while to figure out
1 Like
I think we should probably provide a toBlob for each scalar type (littelendian) and, separately, direct indexing into Blobs.
Would that be better?
I actually have most of the compiler support for that available already.
4 Likes
yes! I guess- I’ll trust your judgement but I just dont like writing code like that with multiple conversion functions.
Sorry, probably ignore this I realised when I wrote this I understood less about motoko than I do now. The main thing was that Nat >> Nat didn’t work and Nat8 >> Nat8 did . Wasn’t clear.
1 Like