Oppolzer - Informatik / Stanford Pascal Compiler


Home       Lebenslauf       Schwerpunkte       Kenntnisse       Seminare       Kunden       Projekte       Produkte       Blog       Stanford Pascal       Kontakt

The Stanford Pascal Compiler / Evolution Steps

Back to Compiler main page

Integer and character constants in hex and binary representation

Compiler version: 08.2017

Although character constants written in hex normally may result in portability problems, some utilities need such facilities. For example, I recently had to write a program on the mainframe acting as a HTTP client talking to a web server, and the answers are in ASCII code (or UTF-8), and so the parsing of the HTTP header involves some looking for X'0a' etc.; even on the mainframe.

Or: if you want to define code tables, that is: char arrays with constant initializers, hex notation is very convenient.

I added X'hh' and B'bbbbbbbb' as another possibility to specify character constants; you may add underscores to improve readability (for example to separate 4 or 8 bit groups in longer constants).

Some versions earlier, 0xhhhh has already been added as another notation for integer constants.

Now there are several problems or pitfalls:

- a program containing a mixture of "normal" character constants and "hex" character constants in a case statement (for example) will probably not compile on all platforms

- same goes for set constants or other expressions involving range expressions

Please look at the following example:


program TESTXB ( OUTPUT ) ; type SET_CHAR = set of CHAR ; var C : CHAR ; S1 : SET_CHAR ; S2 : SET_CHAR ; S3 : SET_CHAR ; S4 : SET_CHAR ; begin (* HAUPTPROGRAMM *) C := 'A' ; WRITELN ( 'c = <' , C , '>' , ORD ( C ) ) ; C := X'31' ; WRITELN ( 'c = <' , C , '>' , ORD ( C ) ) ; C := B'110011' ; WRITELN ( 'c = <' , C , '>' , ORD ( C ) ) ; case C of '1' : WRITELN ( 'eins' ) ; '5' : WRITELN ( 'fuenf' ) ; X'31' : WRITELN ( 'nochmal eins' ) ; X'32' : WRITELN ( 'nochmal zwei' ) ; X'33' : WRITELN ( 'drei als hex' ) ; B'1000110' : WRITELN ( 'f binaer' ) ; X'41' .. 'E' : WRITELN ( 'a als hex' ) ; end (* case *) ; WRITELN ( X'4142434445_414b4c4d4e' ) ; WRITELN ( B'01001111_01000011' ) ; S1 := [ 'A' .. 'E' ] ; S2 := [ X'41' .. 'E' ] ; S4 := [ 'A' .. X'45' ] ; end (* HAUPTPROGRAMM *) .

This will not work on an ASCII machine, because the two case labels '1' and x'31' are obviously the same, but only in ASCII. On an EBCDIC machine, the expression 'A' .. X'45' will not work, because the internal value of 'A' is much higher than X'45'. You will get syntax error on both platforms.

In a future release, the compiler will give warnings on mixed uses of normal and hex or binary notation of char constants that may lead to portability problems.

I hope that I discovered all places in the compiler where character constants are relevant; please feel free to contact me if you detect any errors introduced by this extension. Thanks in advance!

Back to Compiler main page