Path: utzoo!utgpu!water!watmath!clyde!rutgers!cmcl2!brl-adm!brl-smoke!gwyn From: gwyn@brl-smoke.ARPA (Doug Gwyn ) Newsgroups: comp.lang.c Subject: Re: Hex escapes in strings Message-ID: <7021@brl-smoke.ARPA> Date: 11 Jan 88 07:10:34 GMT References: <2938@zeus.TEK.COM> Reply-To: gwyn@brl.arpa (Doug Gwyn (VLD/VMB) ) Organization: Ballistic Research Lab (BRL), APG, MD. Lines: 27 In article <2938@zeus.TEK.COM> dant@tekla (Dan Tilque) writes: >A question occured to me about hex escapes. Where does the padding nybble >go when an odd number of hex digits are in the escaped string? For example: > "\x1A2B3 example" >The escaped constant has 5 hex digits which fit into 2.5 bytes. No! First, it is not specified how big a char (byte) must be, except that it must be AT LEAST 8 bits. It could be much larger, although 16 bits is the largest I expect to find in any C implementation in the near future. Next, the escape \x1A2B3 represents a SINGLE char, not a sequence of chars. If the number is too large to fit in a single char, as it would be for chars through 16 bits in size, then how it is interpreted (still as a SINGLE char) is implementation- defined. Generally, excess high-order bits are discarded, although that is not required. The corresponding character literal '\x1A2B3' is, as always, an int (NOT a char, no matter how small the value). Again, if the value doesn't fit within a SINGLE char, the interpretation is up to the implementation. Generally, the overflow bits are used to assemble additional char subfields within the int, but again that is not required. Portable C programming requires that one not use such over-long character literals. Note that long hex escapes are intended for non-portable usage, primarily in multi-byte character set environments, although they are useful on unusual architectures having chars > 8 bits.