Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Getting [u64; 8] for [u8; 32] in Rust

I need a little code snippet and maybe if someone has time an explanation.

I have a hash function that I use using two different programming languages. The results are then compared in Rust.

What I want to get back is [3246643754, 1918081665, 2401493466, 600956609, 3662614728, 2858571937, 2319608302, 3673956376] so a vector of 8 u64 numbers. This is what the first usage of the hash function returns (I assume it is correct).

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The same hash functions returns – used on a different part in the code –

[42, 218, 131, 193, 129, 154, 83, 114, 218, 225, 35, 143, 193, 222, 209, 35, 200, 16, 79, 218, 161, 88, 98, 170, 238, 105, 66, 138, 24, 32, 252, 218]

so an array of 32 numbers u8.

How can I now convert the latter into the former to compare them?

>Solution :

It looks like you’re casting big-endian u8s to u32 and then storing them as u64.

You can convert each slice of 4 u8s to u32 using u32::from_be_bytes, then cast to u64.

let num2: u64 = u64::from_be_bytes([193, 131, 218, 42]);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading