Skip to content

Commit

Permalink
feat(binary): add byte conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Feb 7, 2020
1 parent 13b6835 commit 564310b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
20 changes: 20 additions & 0 deletions packages/binary/src/bytes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
export const bytes16 = (x: number, le = false) => {
const b0 = x & 0xff;
const b1 = (x >> 8) & 0xff;
return le ? [b0, b1] : [b1, b0];
};

export const bytes24 = (x: number, le = false) => {
const b0 = x & 0xff;
const b1 = (x >> 8) & 0xff;
const b2 = (x >> 16) & 0xff;
return le ? [b0, b1, b2] : [b2, b1, b0];
};

export const bytes32 = (x: number, le = false) => {
const b0 = x & 0xff;
const b1 = (x >> 8) & 0xff;
const b2 = (x >> 16) & 0xff;
const b3 = (x >> 24) & 0xff;
return le ? [b0, b1, b2, b3] : [b3, b2, b1, b0];
};
1 change: 1 addition & 0 deletions packages/binary/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from "./api";
export * from "./align";
export * from "./bytes";
export * from "./count";
export * from "./edit";
export * from "./float";
Expand Down

0 comments on commit 564310b

Please sign in to comment.