Skip to content

Commit 04dd181

Browse files
authored
Merge pull request #425 from caseywescott/asciiToHex
asciiToHex implementation
2 parents 18cb604 + d321397 commit 04dd181

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

packages/utils/src/_test_/utils/index.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
getComponentNameFromEvent,
88
getSelectorFromTag,
99
splitEventTag,
10+
asciiToHex,
1011
} from "../../utils/index";
1112

1213
describe("utils", () => {
@@ -126,4 +127,14 @@ describe("utils", () => {
126127
"/dns4/api.cartridge.gg/tcp/443/x-parity-wss/%2Fx%2Fwordle-game%2Ftorii%2Fwss"
127128
);
128129
});
130+
131+
it("should convert ASCII strings to hex", () => {
132+
const f = (input: string, expected: string) => {
133+
expect(asciiToHex(input)).toBe(expected);
134+
};
135+
136+
f("Hello", "0x48656c6c6f");
137+
f("A", "0x41");
138+
f("123", "0x313233");
139+
});
129140
});

packages/utils/src/utils/index.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,18 @@ export function hexToAscii(hex: string): string {
190190
return str;
191191
}
192192

193+
/**
194+
* Converts an ASCII string to a hexadecimal string.
195+
* With a 0x prefix, matching the original function's format
196+
*/
197+
export function asciiToHex(str: string): string {
198+
let hex = "0x";
199+
for (let i = 0; i < str.length; i++) {
200+
hex += str.charCodeAt(i).toString(16).padStart(2, "0");
201+
}
202+
return hex;
203+
}
204+
193205
/**
194206
* Get the component name from felt event name
195207
*/

0 commit comments

Comments
 (0)