Announcement

Collapse
No announcement yet.

decimal to binary conversion

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • decimal to binary conversion

    Hi,

    Is there a simple function or code to convert decimals to binary and vice versa?

    thanks in advance,

    Izak

  • #2
    Hello Izak,

    I did a search on google and found some code that should work for you. Try the following.

    NumberConverter.efs



    PHP Code:
    /*******************************************************
    CONVERSIONS
    By Ryan Parman
    Distributed according to SkyGPL 2.1, [url]http://www.skyzyx.com/license/[/url]
    *******************************************************/

    function preMain() {
        
    setStudyTitle("Number Converter");
        
    setShowCursorLabel(false);
    }

    function 
    main() {
        if (
    getCurrentBarIndex() == -1) {
            var 
    num = new decimal(181).toBinary();
            
    debugPrintln("181 to binary = " num);
            
            
    debugPrintln(num " to decimal = " + new binary(num).toDecimal());
        }
        
        return;
    }

    function 
    decimal(dec)
    {
        
    this.dec=dec;
        
    this.toBinary=function() { return this.dec.toString(2); }
        
    this.toHex=function() { return this.dec.toString(16).toUpperCase(); }
        
    this.toOctal=function() { return this.dec.toString(8); }
    }

    function 
    binary(bin)
    {
        
    this.bin=bin;
        
    this.toDecimal=function() { return parseInt(this.bin2); }
        
    this.toHex=function() { return this.toDecimal().toString(16).toUpperCase(); }
        
    this.toOctal=function() { return this.toDecimal().toString(8); }
    }

    function 
    hex(hex)
    {
        
    this.hex=hex;
        
    this.toDecimal=function() { return parseInt(this.hex16); }
        
    this.toBinary=function() { return this.toDecimal().toString(2); }
        
    this.toOctal=function() { return this.toDecimal().toString(8); }
    }

    function 
    octal(oct)
    {
        
    this.oct=oct;
        
    this.toDecimal=function() { return parseInt(this.oct8); }
        
    this.toBinary=function() { return this.toDecimal().toString(2); }
        
    this.toHex=function() { return this.toDecimal().toString(16).toUpperCase(); }

    Jason K.
    Project Manager
    eSignal - an Interactive Data company

    EFS KnowledgeBase
    JavaScript for EFS Video Series
    EFS Beginner Tutorial Series
    EFS Glossary
    Custom EFS Development Policy

    New User Orientation

    Comment

    Working...
    X