Skip to main content
Known Participant
May 7, 2022
Question

Convert text file to integers

  • May 7, 2022
  • 2 replies
  • 1048 views

I have a text file with numbers in it. So what I want to know is this.

How can i convert them into integers?

    This topic has been closed for replies.

    2 replies

    Colin Holgate
    Inspiring
    May 7, 2022

    If it is ActionScript, you can type cast the string to be a number. This for example:

    trace("123"+4);

    will give you "1234". But this:

    trace(Number("123")+4);

    will give you 127.

    For HTML5 Canvas you can use JavaScript functions to get the same results. So,:

    alert("123"+4);

    will show "1234", but:

    alert(parseInt("123")+4);

    will show 127.

    Known Participant
    January 20, 2023

    How do I do the parseInt function with this textfile in Animate CC?

    Or Actionscript3?

    JoãoCésar17023019
    Community Expert
    Community Expert
    January 20, 2023

    Hi.

     

    Flash Player and AIR:

    import flash.events.Event;
    import flash.net.URLLoader;
    import flash.net.URLRequest;
    
    var loader:URLLoader = new URLLoader(new URLRequest("sample.txt"));
    
    function completeHandler(e:Event):void
    {
        var loader:URLLoader = e.target as URLLoader;
    	var integer:int = int(loader.data);
    	
    	trace(integer);
    }
    
    loader.addEventListener(Event.COMPLETE, completeHandler);

     

    AIR only:

    import flash.events.Event;
    import flash.filesystem.File;
    import flash.utils.ByteArray;
    
    // the text file is in the documents directory
    var file:File = File.documentsDirectory.resolvePath("sample.txt");
    
    function completeHandler(event:Event):void 
    { 
        var bytes:ByteArray = file.data;
        var str:String = bytes.readUTFBytes(bytes.length);
    	var strToInteger:int = int(str);
    	
        trace(strToInteger);
    }
    
    file.addEventListener(Event.COMPLETE, completeHandler); 
    file.load(); 

     

    I hope it helps.

     

    Regards,

    JC

    JoãoCésar17023019
    Community Expert
    Community Expert
    May 7, 2022

    Hi.

     

    In AS3 or HTML5 Canvas?

     

    Regards,

    JC

    Known Participant
    May 9, 2022

    In AS3