Hexadecimalとは?開発者向けHexガイド
Hexadecimalはprogrammingのあちこちに登場します。color codes、memory addresses、MAC addressesなどで使われるbase-16 number systemの基本をまとめます。
目次
Hexadecimalとは?
Hexadecimalは、よくhexと略されるbase-16 number systemです。decimalが0-9の10 digitsを使うのに対し、hexadecimalは0-9にA-Fを加えた16 symbolsを使います。
語源は、sixを意味するGreekのhexaと、tenを意味するLatinのdecemです。文字通りsix-ten、つまりsixteenを表します。
programmingでは、hex valuesはよく0x prefix付きで書かれます。たとえば0xFFはdecimalでは255です。
なぜHexadecimalを使うのか?
Computersはbinaryで動作しますが、binary numbersはすぐに長くなります。decimal value 255はbinaryでは11111111ですが、hexでは単にFFです。
Hexが便利なのは、16が2のpowerだからです。hex digit 1つはbinary digits 4つにぴったり対応し、hex digits 2つは1 byteを表します。
- hex digit 1つは4 binary digits、つまりnibbleに対応します。
- hex digits 2つは1 byte、つまり8 bitsに対応します。
- hexからbinaryへの変換は、bitsを4つずつgroup化するだけです。
- raw binaryよりcompactで読みやすい表記です。
16個のHex Digits
decimalとbinaryの対応を含む、complete hex digit tableです。
| Hex | Decimal | Binary |
|---|---|---|
0 | 0 | 0000 |
1 | 1 | 0001 |
2 | 2 | 0010 |
3 | 3 | 0011 |
4 | 4 | 0100 |
5 | 5 | 0101 |
6 | 6 | 0110 |
7 | 7 | 0111 |
8 | 8 | 1000 |
9 | 9 | 1001 |
A | 10 | 1010 |
B | 11 | 1011 |
C | 12 | 1100 |
D | 13 | 1101 |
E | 14 | 1110 |
F | 15 | 1111 |
A-Fはcase-insensitiveです。0xff、0xFF、0XFFは同じvalueを表します。
Hex Numbersの読み方
Hexはdecimalと同じように動作しますが、各positionは10ではなく16のpowerです。
Decimal place values: ... 1000 100 10 1
10^3 10^2 10^1 10^0
Hex place values: ... 4096 256 16 1
16^3 16^2 16^1 16^0
Example: 0x2F4
2 x 256 = 512
F x 16 = 240 (F = 15, so 15 x 16)
4 x 1 = 4
-----
Total = 756 (decimal)HexからDecimalへの変換
hexをdecimalに変換するには、各digitにpositionごとの16のpowerを掛け、結果を足します。
Example: 0x1A3
1 x 16^2 = 1 x 256 = 256
A x 16^1 = 10 x 16 = 160
3 x 16^0 = 3 x 1 = 3
-----
Result: 419
So 0x1A3 = 419 in decimal.覚えておくと便利なcommon valuesです。
- 0xFF = 255。1 byteの最大値
- 0x100 = 256
- 0xFFFF = 65,535。16-bit unsigned valueの最大値
- 0x7FFFFFFF = 2,147,483,647。32-bit signed integerの最大値
DecimalからHexへの変換
decimalをhexへ変換するには、16で繰り返し割り、remaindersを記録します。
Example: Convert 750 to hex
750 / 16 = 46 remainder 14 (E)
46 / 16 = 2 remainder 14 (E)
2 / 16 = 0 remainder 2 (2)
Read remainders bottom-to-top: 2EE
So 750 = 0x2EEHexとBinary
hexの本当の強みはbinaryとの対応がきれいなことです。hex digit 1つは正確に4 bitsなので、変換はgroup化だけで済みます。
Hex to Binary:
0xA7 -> A = 1010, 7 = 0111 -> 10100111
Binary to Hex:
11011110 -> 1101 = D, 1110 = E -> 0xDE
Byte example:
0xFF -> 11111111 (all bits on, max byte value)
0x00 -> 00000000 (all bits off, zero)そのためhexは、hex dumps、memory viewers、debuggersでraw bytesを見るためのpreferred notationになっています。
実際に使われるHex
Hexadecimalはcomputingとdevelopmentのさまざまな場所に登場します。
CSS/HTML Colors
Colorsはred、green、blueの3 bytesとして表され、hexで書かれます。
| Color | Hex Code | RGB Values | Preview |
|---|---|---|---|
| Red | #FF0000 | 255, 0, 0 | |
| Green | #00FF00 | 0, 255, 0 | |
| Blue | #0000FF | 0, 0, 255 | |
| White | #FFFFFF | 255, 255, 255 | |
| Black | #000000 | 0, 0, 0 |
その他のよくある用途
- Memory addresses - debuggersは
0x7FFF5FBFF8A0のようなaddressesを表示します。 - MAC addresses -
00:1A:2B:3C:4D:5Eのようなnetwork hardware IDs。 - Unicode code points - charactersは
U+1F600のように識別されます。 - Error codes -
0x80070005のようなWindows HRESULT codes。 - Cryptographic hashes - SHA-256やMD5のoutputはよくhexで表示されます。
- IPv6 addresses - hexadecimal groupsで書かれます。
CodeでのHex
主要なprogramming languageは、hex literalsとconversionをsupportしています。
JavaScript
// Hex literals
const value = 0xFF; // 255
// Decimal to hex string
const hex = (255).toString(16); // "ff"
// Hex string to decimal
const dec = parseInt("ff", 16); // 255
// Pad to 2 digits (useful for colors)
const padded = (10).toString(16).padStart(2, "0"); // "0a"Python
# Hex literals
value = 0xFF # 255
# Decimal to hex string
hex_str = hex(255) # '0xff'
formatted = f"{255:02x}" # 'ff'
# Hex string to decimal
dec = int("ff", 16) # 255CSS
/* Full 6-digit hex */
color: #3B82F6;
/* Short 3-digit hex */
color: #FFF; /* same as #FFFFFF */
/* 8-digit hex with alpha transparency */
color: #3B82F680; /* 50% opacity */よくあるHexの間違い
注意したいpitfallsです。
✕ hexとdecimalを混同する
hexの10はdecimalでは16であり、10ではありません。contextが曖昧な場合は0xや#を使いましょう。
✕ zero paddingを忘れる
byteは2 hex digitsで書くべきです。byte-oriented formatsではAだけでなく、0Aと書きます。
✕ Fより後のlettersを使う
Hexで使えるlettersはA-Fだけです。G、H、Zのようなlettersはvalid hex digitsではありません。
✕ CSS colorsで#を忘れる
CSS hex colorsには# prefixが必要です。
Hex Valuesをすぐに変換
無料のHex Converter toolで、hexadecimal、decimal、binary、octalをbrowser内ですぐに相互変換できます。
Hex Converterを試す参考資料
- Mozilla Developer Network. Hexadecimal - MDN Web Docs Glossary. https://developer.mozilla.org/en-US/docs/Glossary/Hexadecimal
- Mozilla Developer Network. <color> - CSS: Cascading Style Sheets. https://developer.mozilla.org/en-US/docs/Web/CSS/color_value
- Mozilla Developer Network. parseInt() - JavaScript. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt
- The Unicode Consortium. Unicode Code Charts. https://www.unicode.org/charts/
- IEEE. IEEE 802 - MAC Address Format. https://standards.ieee.org/products-programs/regauth/