Reputation:
I don’t understand how xa
can be converted to 10
in Borland Pascal. I just use
Val('xa', value, return);
and value
becomes 10
, and return
becomes 0
(meaning successful conversion). I’m just a newbie, can anyone explain this? I know this won’t like the ASCII cause that is just a character.
And I’m using Free Pascal ☺
I tested it in Free Pascal using xa
, 0xa
and $xa
. So, I think Val
understands the special characters like $
or 0
without me needing to call specialized functions like Hex2Dec
. Is that right?
Upvotes: 0
Views: 2267
Reputation: 26356
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
Upvotes: 6
Reputation: 1438
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
Upvotes: 2