Convert a number from base A to base B | Function to return ASCII value of a character ; Function to convert a number from given base to decimal number ; Stores the length of the var ; Initialize power of base ; Initialize result ; Decimal equivalent is str [ len - 1 ] * 1 + str [ len - 2 ] * base + str [ len - 3 ] * ( base ^ 2 ) + ... ; A digit in input number must be less than number 's base ; Update num ; Update power ; Function to return equivalent character of a given valueString . fromCharCode ; Function to convert a given decimal number to a given base ; Store the result ; Repeatedly divide inputNum by base and take remainder ; Update res ; Update inputNum ; Reverse the result ; Function to convert a given number from a base to another base ; Convert the number from base A to decimal ; Convert the number from decimal to base B ; Prvar the result ; Given input ; Function Call
function val ( c ) { if ( c >= ' ' && c <= ' ' ) return c . charCodeAt ( 0 ) - 48 ; else return c . charCodeAt ( 0 ) - 65 + 10 ; } function toDeci ( str , base ) { var len = str . length ; var power = 1 ; var num = 0 ; for ( var i = len - 1 ; i >= 0 ; i -- ) { if ( val ( str [ i ] ) >= base ) { document . write ( " " ) ; return - 1 ; } num += val ( str [ i ] ) * power ; power = power * base ; } return num ; } function reVal ( num ) { if ( num >= 0 && num <= 9 ) return String . fromCharCode ( num + 48 ) ; else return String . fromCharCode ( num - 10 + 65 ) ; } function fromDeci ( base , inputNum ) { var res = " " ; while ( inputNum > 0 ) { res += reVal ( inputNum % base ) ; inputNum = Math . floor ( inputNum / base ) ; } res = res . split ( " " ) . reverse ( ) . join ( " " ) ; return res ; } function convertBase ( s , a , b ) { var num = toDeci ( s , a ) ; var ans = fromDeci ( b , num ) ; document . write ( ans ) ; } var s = " " ; var a = 16 var b = 10 ; convertBase ( s , a , b ) ;