type cast voodoo

hehimself
Posts:9
Joined:Wed Oct 23, 2019 6:22 am
type cast voodoo

Postby hehimself » Thu Sep 09, 2021 1:42 pm

Hi,

could somebody please check the following code (end of post) on an Ember system? I get different results on my 2 systems. The old Debian rack with Ember-1.9 produces the expected output:

digit=0
string=07727
digit=7727
string=7728
digit=7728
and so on

My new Ubuntu rack drives me crazy with

digit=0
string=07727
digit=4055
string=4056
digit=4056
and so on

If I use as starting string "07728" it gets even better:

digit=0
string=07728
Runtime error at line 17 of script 'casting_fun.ehtml'. Error is 'Type cast failed'

Looking at different bases there seems to be a connection with the octal representation. 7727 octal is 4055 decimal. But it's not what I need or expect, especially the type cast failure . . . :-(

So, any hints to whether my machine is broken or my code or Ember are welcome.

<html>
<head>
<title>Casting fun</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>

<body>
<!
$digit = 0;
$count = 0;
$string = "07727";
printf("digit=%d<br>\n", $digit);
printf("string=%s<br>\n", $string);

while ( $count < 11 )
{
$digit = (int)$string;
printf("digit=%d<br>\n", $digit);
$digit = $digit + 1;
$string = (text)$digit;
printf("string=%s<br>\n", $string);
$count = $count + 1;
}
>
</body>
</html>

Regards
Gerhard

david
Hughes Tech Staff
Posts:25
Joined:Mon Feb 13, 2017 4:55 am

Re: type cast voodoo

Postby david » Fri Sep 10, 2021 5:58 am

This works as expected with the current 2.1 release. Also, if you strip all the HTML stuff from it you can just run it as you would any "normal" script using "ember FILENAME"

As for type casting from string to integer, we just use whatever your system includes. To get an understanding have a read of

Code: Select all

man strtol

David
...