Non-decimal radices/Convert: Difference between revisions

From Rosetta Code
Content added Content deleted
(Added Java example)
(Fixed task description English and added more syntax highlighting)
Line 1: Line 1:
{{task}}[[Category:Number Base Conversion]]Number Base Conversion is when you express a stored integer in a number base, such as in octal base 8 or binary base 2. It also is involved when you take a string representing a number in a given base and convert it to the stored integer form. Normally a stored integer is in binary, but that's typically invisible to the user, who normally enters or sees stored integers as decimal.
{{task}}Number base conversion is when you express a stored integer in an integer base, such as in octal (base 8) or binary (base 2). It also is involved when you take a string representing a number in a given base and convert it to the stored integer form. Normally, a stored integer is in binary, but that's typically invisible to the user, who normally enters or sees stored integers as decimal.


Write a function (or identify the built-in function) which is passed a non-negative integer to convert, and another integer representing the base. It returns a string containing the digits of the resulting number, without leading zeros except for the number 0 itself. For the digits beyond 9, one should use the lowercase english alphabet, where the digit a = 9+1, b = a+1, etc. The decimal number 26 expressed in base 16 would be 1a, for example.
Write a function (or identify the built-in function) which is passed a non-negative integer to convert, and another integer representing the base. It should return a string containing the digits of the resulting number, without leading zeros except for the number 0 itself. For the digits beyond 9, one should use the lowercase English alphabet, where the digit a = 9+1, b = a+1, etc. The decimal number 26 expressed in base 16 would be 1a, for example.


Write a second function which is passed a string and a base, and it returns an integer representing that string interpreted in that base.
Write a second function which is passed a string and an integer base, and it returns an integer representing that string interpreted in that base.


The programs may be limited by the word size or other such constraint of a given language. There is no need to do error checking for negatives, bases less than 2, or inappropriate digits.
The two functions should each accept the output of the other. For example, k = 123456789; m = int(baseN(k,3),3); gives m==k.
The programs may be limited by the word size or other such constraint of a given language, and do not need to do error checking for negatives, bases less than 2, or inappropriate digits.


=={{header|Python}}==
=={{header|Python}}==
<pre>
<python>
def baseN(num,b):
def baseN(num,b):
return ((num == 0) and "0" ) or ( baseN(num // b, b).lstrip("0") + "0123456789abcdefghijklmnopqrstuvwxyz"[num % b])
return ((num == 0) and "0" ) or ( baseN(num // b, b).lstrip("0") + "0123456789abcdefghijklmnopqrstuvwxyz"[num % b])
Line 15: Line 14:
s = baseN(k,16) # returns the string 1a
s = baseN(k,16) # returns the string 1a
i = int('1a',16) # returns the integer 26
i = int('1a',16) # returns the integer 26
</pre>
</python>


=={{header|Java}}==
=={{header|Java}}==

Revision as of 03:08, 7 March 2008

Task
Non-decimal radices/Convert
You are encouraged to solve this task according to the task description, using any language you may know.

Number base conversion is when you express a stored integer in an integer base, such as in octal (base 8) or binary (base 2). It also is involved when you take a string representing a number in a given base and convert it to the stored integer form. Normally, a stored integer is in binary, but that's typically invisible to the user, who normally enters or sees stored integers as decimal.

Write a function (or identify the built-in function) which is passed a non-negative integer to convert, and another integer representing the base. It should return a string containing the digits of the resulting number, without leading zeros except for the number 0 itself. For the digits beyond 9, one should use the lowercase English alphabet, where the digit a = 9+1, b = a+1, etc. The decimal number 26 expressed in base 16 would be 1a, for example.

Write a second function which is passed a string and an integer base, and it returns an integer representing that string interpreted in that base.

The programs may be limited by the word size or other such constraint of a given language. There is no need to do error checking for negatives, bases less than 2, or inappropriate digits.

Python

<python> def baseN(num,b):

  return ((num == 0) and  "0" ) or ( baseN(num // b, b).lstrip("0") + "0123456789abcdefghijklmnopqrstuvwxyz"[num % b])

k = 26 s = baseN(k,16) # returns the string 1a i = int('1a',16) # returns the integer 26 </python>

Java

<java>public static int backToTen(String num, int oldBase){

  return Integer.parseInt(num, oldBase); //takes both uppercase and lowercase letters

}

public static String tenToBase(int num, int newBase){

  return Integer.toString(num, newBase);//add .toUpperCase() for capital letters

}</java>

JavaScript

k = 26
s = k.toString(16) //gives 1a
i = parseInt('1a',16) //gives 26
//optional special case for hex:
i = +('0x'+s) //hexadecimal base 16, if s='1a' then i=26.