SRL Archive - Help

Opposite of chr(''); ?


weequ

2012-12-31 11:55:15

So with for example chr(35) I can create 'a' character but what function I can use to convert that a back to 35? GetKeyCode wont work because it will return 35 for both 'a' and 'A' and it also doesn't work for character codes above 128 (always returns 255).

Edit: Nvm found that Ord('a') does the trick.

masterBB

2012-12-31 12:10:19

Another solution:

myChar as Integer

weequ

2012-12-31 12:16:47

Wow i've never seen the as keyword. Thanks :)

footballjds

2012-12-31 13:03:35

From: masterBB
Another solution:

myChar as Integer


can you show it's use? I've never seen this

Brandon

2012-12-31 13:06:26

It's like casting in pascal script.


var
M: Char;
begin
M := 'a';

writeln(M As Integer); //prints 97. The ascii value corresponding to character a.
end.


var
C: Char;
I: Integer;
begin
C := 'a';
I := C As Integer;
writeln(I);
end.

masterBB

2012-12-31 13:26:52

Note that it works both ways. So instead of calling chr(97):

97 as Char

edit:
since lape doesn't support as. Does anybody knows a better way then casting like this?

var
myChar: Char;
memoryPointer: Pointer;

begin
myChar := Chr(97);
memoryPointer := @myChar;
writeln(Integer(memoryPointer^));
end.

nielsie95

2012-12-31 13:50:40

As is used to cast classes. You can use Ord() to get the ordinal value or cast it to a type of the same size (Byte). Your pointer cast is actually wrong because you are reading 4 bytes (size of integer) where only 1 byte is used (Char).

Ord() is the best solution :)

masterBB

2012-12-31 13:52:32

From: nielsie95
As is used to cast classes. You can use Ord() to get the ordinal value or cast it to a type of the same size (Byte).


Thanks ^^

var
myChar: Char;

begin
myChar := Chr(97);
writeln(Byte(myChar));
end.

footballjds

2012-12-31 14:07:47

Thank you guys!

Brandon

2012-12-31 14:07:47


var
C: Char;
I: Integer;
begin
C := 'a';
I := Integer(Pointer(@C)^);
writeln(I);

C := Char(Pointer(@I)^);
writeln(C);
end.

Looks disgusting LOL.. :l

nielsie95

2012-12-31 14:17:39

Because a character is smaller than an integer, the result might not be what you expect it to be:


var
s = 'abc';
begin
WriteLn(s[1], ': ', Ord(s[1]), '=', Byte(s[1]), ' <> ', Integer(Pointer(@s[1])^));
WriteLn(SizeOf(Char), ' <> ', SizeOf(Integer));
end.

Ord() will do just fine! :)