A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: Why is number 8 equal to 010 ??

  1. #1
    Member
    Join Date
    Aug 2014
    Posts
    51

    Why is number 8 equal to 010 ??

    Hi all,

    If you try out this simple piece of code below :

    Code:
    var i:Number;
    
    for(i=0;i<20;i++)
    {
    	if(i === 010){
    		trace("yes");
    	}else{
    		trace("no");
    	}
    }

    you will find that for 010 === 8, yes is echoed, for 011===10 yes is echoed too and I bet there would be more such values too.

    Any ideas why this is so ? How to get around this issue ?

    Thanks all !

  2. #2
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,785
    Hi,

    It seems you are entering the bounds of Binary Number System although what you have done is wrong, I know not why.
    https://www.mathsisfun.com/binary-number-system.html

    Try this code and see what it traces

    PHP Code:
    var i:Number;
    var 
    output:String;

    for (
    026i++)
    {
        if (
    i.toString(2) === "1000")
        {
            
    output "Positive";
        }
        else
        {
            
    output "Negative";
        }
        
    trace("\t:\t" output " - " i.toString(2));

    Perhaps I'm mistaken too, look into octal numebrs !!!
    Last edited by fruitbeard; 10-01-2015 at 11:29 AM.

  3. #3
    Tim (Super Moderator) Northcode's Avatar
    Join Date
    Dec 2000
    Location
    Whitehorse YT
    Posts
    3,766
    010 is 8 in octal, check out http://docstore.mik.ua/orelly/web2/action/ch04_03.htm for an explanation of numeric literals in ActionScript
    When your swf2exe tool just HAS to work
    there's only one choice... SWF Studio

  4. #4
    Member
    Join Date
    Aug 2014
    Posts
    51
    Hi fruitbeard and Northcode. Thanks for that insight. It's the octal 8 but i am surprised that a notation is not used like the leading 'x' in to denote the hexdecimal numbers. Is there a way to indicate to the system that the numbers being input are not octal?

    If I am taking an input from a user and I wish to check if it's the number 8 then can't the user simply fool the system by adding 010 instead? I don't think that there is a way - at least an elegant way - to disallow the first character to be a 0 in an "input box". So if I am checking for the number 8, I guess the only way around would be to get the input text and then strip off any leading zeros.

    Am i thinking correct or is there a better solution to this ?
    Thanks loads.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center