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

Adding toBytes and fromBytes to Nat32 #269

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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: 17 additions & 0 deletions src/Nat32.mo
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
///
/// Most operations are available as built-in operators (e.g. `1 + 1`).
import Nat "Nat";
import Nat8 "Nat8";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this module actually used?

import Prim "mo:⛔";

module {
Expand Down Expand Up @@ -143,4 +144,20 @@ module {
/// Returns `x` to the power of `y`, `x **% y`. Wraps on overflow.
public func powWrap(x : Nat32, y : Nat32) : Nat32 { x **% y };

/// Returns [Nat8] of size 4 of the Nat32
public func toBytes(x : Nat32) : [Nat8] {
[ Prim.natToNat8(Prim.nat32ToNat((x >> 24) & (255))),
Prim.natToNat8(Prim.nat32ToNat((x >> 16) & (255))),
Prim.natToNat8(Prim.nat32ToNat((x >> 8) & (255))),
Prim.natToNat8(Prim.nat32ToNat((x & 255))) ];
Comment on lines +149 to +152
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit:

Suggested change
[ Prim.natToNat8(Prim.nat32ToNat((x >> 24) & (255))),
Prim.natToNat8(Prim.nat32ToNat((x >> 16) & (255))),
Prim.natToNat8(Prim.nat32ToNat((x >> 8) & (255))),
Prim.natToNat8(Prim.nat32ToNat((x & 255))) ];
[ Prim.natToNat8(Prim.nat32ToNat(x >> 24 & 255)),
Prim.natToNat8(Prim.nat32ToNat(x >> 16 & 255)),
Prim.natToNat8(Prim.nat32ToNat(x >> 8 & 255)),
Prim.natToNat8(Prim.nat32ToNat(x & 255)) ];

};

/// Returns Nat32 for a four byte array
public func fromBytes(x : [Nat8] : Nat32] {
(Prim.natToNat32(Prim.nat8ToNat(x[0])) << 24) +
(Prim.natToNat32(Prim.nat8ToNat(x[1])) << 16) +
(Prim.natToNat32(Prim.nat8ToNat(x[2])) << 8) +
(Prim.natToNat32(Prim.nat8ToNat(x[3])));
Comment on lines +157 to +160
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
(Prim.natToNat32(Prim.nat8ToNat(x[0])) << 24) +
(Prim.natToNat32(Prim.nat8ToNat(x[1])) << 16) +
(Prim.natToNat32(Prim.nat8ToNat(x[2])) << 8) +
(Prim.natToNat32(Prim.nat8ToNat(x[3])));
Prim.natToNat32(Prim.nat8ToNat(x[0])) << 24 +
Prim.natToNat32(Prim.nat8ToNat(x[1])) << 16 +
Prim.natToNat32(Prim.nat8ToNat(x[2])) << 8 +
Prim.natToNat32(Prim.nat8ToNat(x[3]));

};

}
18 changes: 18 additions & 0 deletions test/nat32Test.mo
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import Debug "mo:base/Debug";
import Nat "mo:base/Nat";
import Nat8 "mo:base/Nat8";
import Nat32 "mo:base/Nat32";

Debug.print("Nat32");

do {
Debug.print(" toBytes");

assert(Nat32.toBytes((4_123_435_857:Nat32)) == [(245:Nat8), (198:Nat8), (163:Nat8), (81:Nat8)]);
};

do {
Debug.print(" fromBytes");

assert(Nat32.fromBytes([(245:Nat8), (198:Nat8), (163:Nat8), (81:Nat8)]) == (4_123_435_857:Nat32))
};