JavaScript theorytheory 0/50 · 0%
Strings · medium

34. Strings, templates and encoding

Hex, bytes and UTF-8.

Template literals interpolate and span lines. Blockchain work constantly converts between UTF-8 text, bytes and hex strings — remember a hex byte is two characters plus the `0x` prefix.

const bytes = new TextEncoder().encode("gm");
const hex = "0x" + [...bytes].map((b) => b.toString(16).padStart(2, "0")).join("");

Compare addresses case-insensitively unless you are validating an EIP-55 checksum.

Check your understanding

  1. 1. How many hex characters per byte?

  2. 2. What does TextEncoder produce?

  3. 3. How should addresses be compared?