# prefer-uint8array-hex

Rule `unicorn/prefer-uint8array-hex`. Prefer `Uint8Array#toHex()` and `Uint8Array.fromHex()` over manual and Buffer hex conversions.

- Source: unicorn
- Technologies: JavaScript
- Kind: style
- Needs type information: no
- Autofix: yes, in at least one linter
- Reach: 0 weekly downloads of presets that enable it
- Trend: 0% last quarter
- Rank: #2040 by reach

## Linters

| Linter | Rule name | Recommended | Autofix | Details | Docs |
| --- | --- | --- | --- | --- | --- |
| ESLint | `unicorn/prefer-uint8array-hex` | no | safe | package `eslint-plugin-unicorn` | [docs](https://github.com/sindresorhus/eslint-plugin-unicorn/blob/v80.0.0/docs/rules/prefer-uint8array-hex.md) |

## Code examples

### Incorrect: the rule reports this

```js
const hex = [...bytes].map(byte => byte.toString(16).padStart(2, '0')).join('');
```

```js
const hex = Array.from(bytes).map(byte => byte.toString(16).padStart(2, '0')).join('');
```

```js
const hex = Array.from(bytes, byte => byte.toString(16).padStart(2, '0')).join('');
```

### Correct: the rule accepts this

```js
const hex = bytes.toHex();
```

```js
const hex = bytes.toHex();
```

```js
const hex = buffer.toHex();
```

Examples from the ESLint documentation.

## Enable it

### ESLint: `eslint.config.js`

```js
import unicorn from "eslint-plugin-unicorn";

export default [
  {
    plugins: { unicorn },
    rules: {
      "unicorn/prefer-uint8array-hex": "error",
    },
  },
];
```

## Related rules

- [unicorn/prefer-uint8array-base64](https://lintradar.dev/rules/unicorn/prefer-uint8array-base64.md): Prefer `Uint8Array#toBase64()` and `Uint8Array.fromBase64()` over legacy base64 conversions and manual postprocessing.
- [unicorn/prefer-temporal](https://lintradar.dev/rules/unicorn/prefer-temporal.md): Prefer `Temporal` over `Date`.
- [unicorn/require-frontmatter-fields](https://lintradar.dev/rules/unicorn/require-frontmatter-fields.md): Require configured YAML frontmatter fields.
- [unicorn/prefer-short-arrow-method](https://lintradar.dev/rules/unicorn/prefer-short-arrow-method.md): Prefer arrow function properties over methods with a single return.
- [unicorn/string-content](https://lintradar.dev/rules/unicorn/string-content.md): Enforce better string content.
- [unicorn/prefer-regexp-escape](https://lintradar.dev/rules/unicorn/prefer-regexp-escape.md): Prefer `RegExp.escape()` for escaping strings to use in regular expressions.

---

Web page: https://lintradar.dev/rules/unicorn/prefer-uint8array-hex. Data from npm and the linters' documentation, collected Dec 4, 2026. Site guide for agents: https://lintradar.dev/llms.txt
