Online Documentation Server
 ПОИСК
ods.com.ua Web
 КАТЕГОРИИ
Home
Programming
Net technology
Unixes
Security
RFC, HOWTO
Web technology
Data bases
Other docs

 


 ПОДПИСКА

 О КОПИРАЙТАХ
Вся предоставленная на этом сервере информация собрана нами из разных источников. Если Вам кажется, что публикация каких-то документов нарушает чьи-либо авторские права, сообщите нам об этом.




Previous Table of Contents Next

This function accepts a decimal integer and returns its representation in the specified base. The function uses the algorithm explained earlier in this chapter. At first, the variable newNum is declared and initialized with an empty string. The conditional expression assigns the value “0” to the variable if the value of num is 0. Another version of the same function is as follows:

function toBase(num, base) {
 var newNum = ""
 while (num >= base) {
  newNum = getDigit(num % base) + newNum
  num = div(num, base)
 }
 newNum = div(num, base) + newNum
 return newNum
}

Referring again to the original version, the variable newNum holds the number in its new representation. It is vital to assign it a string literal so that we can add digits to its left, as explained in the “Algorithm” section.

The main part of this function is the loop. It extracts the appropriate values from the number and concatenates them to the left of the new one. Let’s take a look at the loop interior. The variable newNum is assigned the new digit concatenated to the left of the existing value of newNum. The string initialization guarantees that all operations on the new number are string operations, enabling us to “attach” strings or number anywhere. The new digit is getDigit(num % base).

Here is the conversion method explained before:

Ma = [...][((num div base) div base) % base][(num div base) % base][num % base]

Each digit has the format [aValue % base]. The first statement in the loop is based on this format. It concatenates the number (num % base) with the existing string, from the left. Note that the number is first sent to the getDigit() function to be converted to its proper notation. Since the modulo operator returns a value which is smaller than its second operator, no digit will be greater than the maximum digit of that base, base.

Look back once again at the equation. Notice the rule related to the value of aValue:

aValue = theNumber
aValue = theNumber div a
aValue = (theNumber div a) div a
aValue = ((theNumber div a) div a) div a
.
.
.

This rule is expressed in the second statement of the loop. The variable num is intrinsically divided by the base in every iteration. Its momentary value is always the correct value of aValue.

The loop condition is num >= 1, because any number less than the base does not need to be converted. The statement num = div(num, base) is at the end of the loop, so the value of num at the beginning of the next iteration is not the same as it was during the previous one. Since x div y always evaluates to 0 when x < y, the value of num will be equal to 0 if it is less than the value of base. The loop condition (num >= 1) ensures that the loop will terminate when all digits have been concatenated.

toDec(num, base)

// Convert from specified base to decimal
/////////////////////////////////////////

function toDec(num, base) {
 if (base == 8)
  return parseInt("0" + num)
 if (base == 16)
  return parseInt("0x" + num)
 num = "" + num // convert to string by casting
 var numLength = num.length
 // the length property returns the length of a string
 var newNum = 0 // initialization
  // (must be 0 so the sum is not affected)
 var curDigit = ""
 var contributedValueValue = 0
 for (var i = numLength – 1; i >= 0; --i) {
curDigit = num.charAt(i)
contributedValue = getValue(curDigit)
contributedValue *= power(base, numLength – (i + 1))
newNum += parseInt(contributedValue)
 }
 return newNum
}

This function returns the value of num in the specified base. It is based on the algorithm explained earlier in the chapter.

At first, the function checks if the number is an octal or hexadecimal one. If so, it returns the number with a leading prefix (0 or 0x). Note that the parseInt() function converts the concatenated string to an integer in a decimal base, so no prior conversions are needed for octal or hexadecimal numbers. If the base is not octal (8) or hexadecimal (16), an empty string is concatenated to the variable num, converting it to a string, regardless of its original type. The function assigns the number to the local variable numLength via the strings property, length. In JavaScript, all strings, as opposed to numbers, are objects. The length property exists only for strings, so we must convert num to a string before we use it. It is also possible to calculate the number of digits in a number via the modulo operator, but that seems to be longer and more complicated.

You may recall from the “Algorithm” section that the equation used to convert a number in any base to a decimal notation is completely arithmetical. For that, a new variable named newNum is declared and initialized to 0. It holds the value of the computed decimal value.

Two more local variables are declared, curDigit, and contributedValue; the first one is initialized to an empty string, and the second to 0. The loop section is straightforward and follows the algorithm explained above. The built-in parseInt() function converts its argument to an integer rather than a string (for example, “11” [rdblarr] 11). The last statement of the function returns the new number.

Previous Table of Contents Next


With any suggestions or questions please feel free to contact us