-1

I don't know how "xa" can convert in to 10 in pascal. I just use:

Val('xa',value,return);

And value = 10, return = 0. I'm just a newbie, anybody can explain this? I know this won't like the ASCII cause that is just a character.

And I'm using Free Pascal :)

I tested in Free Pascal, when use xa, 0xa and $xa. So, I think it understand the special character like "$","0" without calling it. Is that right?

2 Answers2

5

Since early Delphi's, the core integer conversion routines don't do just number sequences, but also some specials like Pascal "$924" for hex or C style 0x02).

FreePascal adopted it when it later started adding Delphi compatibility (roughly 1997-2003). Beside this difference, another different is that the last parameter (RETURN in your example) changed from WORD (in Turbo Pascal) to integer/longint in Delphi.

IOW, the routine accepts the x and thinks you mean to convert a C style hex number, and then interprets the "a" according to Stuart's table.

It also interprets % as binary, and & as octal.

Try

val('$10',value,return);
writeln(value,' ' ,return);  // 16 0
val('&10',value,return);
writeln(value,' ' ,return);  // 8 0
val('%10',value,return);
writeln(value,' ' ,return);  // 2 0

and compare the results.

Note that this probably won't work for very old Pascal's like Turbo Pascal, and Free Pascals from before the year 2000. The % and & are FPC specific to match the literal notation extensions (analogous to $, but for binary and octal)

var x : Integer
begin
x:=%101010;  //42
x:=&101;     //65
Marco van de Voort
  • 25,628
  • 5
  • 56
  • 89
2

This won't work with all Pascal compilers, and you didn't say what Pascal compiler you are using, but it looks like, the x in 'xa' says that this is a hexadecimal (base 16) number, and the value of the digits in a hexadecimal number are as follows:

Digit Value
0      0
1      1
2      2
3      3
4      4
5      5
6      6
7      7
8      8
9      9
a     10
A     10
b     11
B     11
c     12
C     12
d     13
D     13
e     14
E     14
f     15
F     15
Stuart
  • 1,428
  • 11
  • 20
  • I confirm, the same happened to me on windows using StrToInt(): it understood hex numbers out of the box. I am not sure, it was lazarus (so, freepascal) or, perhaps, even Delphi 6? – linuxfan says Reinstate Monica Jan 26 '19 at 09:52
  • The $ and 0x/x stuff is Delphi, at least D4, but wouldn't be surprised D2 too. I never used (16-bit) D1. Probably it supports 0x because of BCB, since the IDE is written in Delphi afaik. – Marco van de Voort Feb 04 '19 at 12:37