Skip to content

Commit

Permalink
Support number[] in uint8ArrayToHexString
Browse files Browse the repository at this point in the history
  • Loading branch information
dskloetd committed Nov 24, 2023
1 parent 6bf9c68 commit 193b775
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
4 changes: 4 additions & 0 deletions packages/utils/src/utils/arrays.utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,8 @@ describe("arrays-utils", () => {
it("should convert hex uint8array to string", () => {
expect(uint8ArrayToHexString(hexArray)).toEqual(hex);
});

it("should convert array of numbers to string", () => {
expect(uint8ArrayToHexString(Array.from(hexArray))).toEqual(hex);
});
});
11 changes: 9 additions & 2 deletions packages/utils/src/utils/arrays.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,12 @@ export const hexStringToUint8Array = (hexString: string): Uint8Array => {
return Uint8Array.from(matches.map((byte) => parseInt(byte, 16)));
};

export const uint8ArrayToHexString = (bytes: Uint8Array) =>
bytes.reduce((str, byte) => str + byte.toString(16).padStart(2, "0"), "");
export const uint8ArrayToHexString = (bytes: Uint8Array | number[]) => {
if (!(bytes instanceof Uint8Array)) {
bytes = Uint8Array.from(bytes);
}
return bytes.reduce(
(str, byte) => str + byte.toString(16).padStart(2, "0"),
"",
);
};

0 comments on commit 193b775

Please sign in to comment.