> For the complete documentation index, see [llms.txt](https://lochiwei.gitbook.io/web/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://lochiwei.gitbook.io/web/js/val/prim/num/number+ext/n.tohex.md).

# n.toHex()

to binary / octal / hex format.

[JS](/web/js.md) ⟩ [value](/web/js/val.md) ⟩ [primitive](/web/js/val/prim.md) ⟩ [number](/web/js/val/prim/num.md) ⟩ [functions](broken://pages/DTJOAKgK1E3m5zlnFz4G) ⟩ n.toHex()

{% hint style="success" %}
convert to binary / octal / hex format (string).
{% endhint %}

{% tabs %}
{% tab title="💾 程式" %}

* replit： [num.toHex() (v.2)](https://replit.com/@pegasusroe/numtoHex-v2#index.js)  (require： [nested default values](/web/js/grammar/op/assign/destruct/nested/nested-default.md))

```javascript
// ⭐ getter/setter
Object.defineProperties(Number.prototype, {

    // n.binary
    binary: {
        get() { return this.toString(2) },
    },

    // n.octal
    octal: {
        get() { return this.toString(8) },
    },

    // n.hex
    hex: {
        get() { return this.toString(16).toUpperCase() },
    },

});

// ⭐ general function
function formatFunc(pre, base) {
    return function format({
        prefix = false,
        pad = { length: 0, char: '0' },
    } = {}) {
        // ⭐ `pad` might be overridden, better destructure again
        const { length = 0, char = '0' } = pad;
        return (prefix ? pre : '') + 
            this.toString(base).padStart(length, char).toUpperCase();
    }
}

// num.toBinary()
Number.prototype.toBinary = formatFunc('0b', 2);

// num.toOctal()
Number.prototype.toOctal = formatFunc('0o', 8);

// num.toHex()
Number.prototype.toHex = formatFunc('0x', 16);

// export nothing
module.exports = {};
```

💈範例：

```javascript
(123).binary,    // '1111011'
(123).octal,     // '173'
(123).hex,       // '7B'

// binary
(12).toBinary(),                        // '1100'
(12).toBinary({ prefix: true }),        // '0b1100'
(12).toBinary({ pad: {length: 10} }),   // '0000001100'

(12).toBinary({                         // '0b0000001100'
    prefix: true,
    pad: { length: 10 },
}),

(12).toBinary({                         // '0b______1100'
    prefix: true,
    pad: { length: 10, char: '_' },
}),

// octal
(123).toOctal(),                        // 173
(123).toOctal({prefix: true}),          // 0o173
(123).toOctal({pad: {length: 10}}),     // 0000000173

(123).toOctal({                         // '0o0000000173'
    prefix: true,
    pad: { length: 10 },
}),

(123).toOctal({                         // '0o.......173'
    prefix: true,
    pad: { length: 10, char: '.' },
}),

// hex
(123).toHex(),                        // 7B
(123).toHex({prefix: true}),          // 0x7B
(123).toHex({pad: {length: 10}}),     // 000000007B

(123).toHex({                         // '0x000000007B'
    prefix: true,
    pad: { length: 10 },
}),

(123).toHex({                         // '0x________7B'
    prefix: true,
    pad: { length: 10, char: '_' },
}),
```

{% endtab %}

{% tab title="👥 相關" %}

* :m:[Object.defineProperty()](/web/js/val/obj/prop/create/object.defineproperty.md)
  {% endtab %}

{% tab title="📗 參考" %}

* [ ] [JavaScript: The Definitive Guide](/web/master/ref/javascript-the-definitive-guide.md) ⟩&#x20;
  * [x] 3.9.2 Explicit Conversions
  * [ ] 11.7.1
    {% endtab %}

{% tab title="📘 手冊" %}

* [Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number) ⟩
  * [.toString()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toString)  ⭐️
  * [.toFixed()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed)
  * [.toExponential()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toExponential)
  * [.toPrecision()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toPrecision)
    {% endtab %}
    {% endtabs %}
