プログラミング言語Cの次期仕様C2yでは、16進数表記で用いられるアルファベット文字範囲'a'~'f'および'A'~'F'について文字コードの連続性保証が明文化される。
この文字コード範囲での連続性保証は、ラテンアルファベット26文字 A~Z / a~z が連続配置されるASCII互換文字コードやUnicodeだけでなく、EBCDIC文字コードにおいても成立する*1。
int ch = /*...*/; assert(isxdigit(ch)); // chは任意の16進文字 int n; if ('0' <= ch && ch <= '9') { n = ch - '0'; // 0...9 } else if ('a' <= ch && ch <= 'f') { n = ch - 'a' + 10; // a...f } else { n = ch - 'A' + 10; // A...F }
C2y WD N3301 5.3.1/p3-5, Annex M.2より一部引用。
3 (snip) In both the source and execution basic character sets, the value of each character after
0in the preceding list of decimal digits shall be one greater than the value of the previous. (snip)
4 The value of each character aftera, up to and includingf, in the prior specified list of lowercase letters shall be one greater than the value of the previous.
5 The value of each character afterA, up to and includingF, in the prior specified list of uppercase letters, shall be one greater than the value of the previous.
Uppercase characters "A" through "F" (
'A'through'F') and lowercase characters "a" through "f" ('a'through'f'), colloquially known as the "hexidecimal digits", are now guaranteed to be sequential.
関連URL