-3

I need to turn this line of Python to Javascript

BYTE_ARRAY = bytearray(b"\x01")

Miguel
  • 23
  • 4
  • It’s `new TextEncoder().encode("\x01")`. – Sebastian Simon Jan 20 '23 at 17:02
  • What *features* do you need from the result? Python 3's strict separation between, and top-level builtins for, raw binary types vs. platonic text aren't as strictly observed in JS, and it's common to just use plain `Array`s. – ShadowRanger Jan 20 '23 at 17:10
  • It's highly likely [How to store a byte array in Javascript](https://stackoverflow.com/q/12332002/364696) and/or [How to convert a String to Bytearray](https://stackoverflow.com/q/6226189/364696) will answer your question, but without knowing what features from Python you'll need in JS, it's hard to say. – ShadowRanger Jan 20 '23 at 17:17

1 Answers1

0

Uint8Array is like the bytearray Python function. It is used to create a byte array from hexadecimal.

const BYTE_ARRAY = new Uint8Array([0x01]);

Docs: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array

iohans
  • 838
  • 1
  • 7
  • 15
  • This is mostly equivalent to Python 3's `bytes` type, but it's fixed length at construction, it's not designed to have elements pushed/popped/concatenated (which is one of the two main reasons you use `bytearray` rather than `bytes` in Python 3, the other being the ability to mutate individual indices), so it's only a partial replacement. – ShadowRanger Jan 20 '23 at 17:13