Non-decimal radices/Convert: Difference between revisions

From Rosetta Code
Content added Content deleted
(sort)
(forth)
Line 63: Line 63:
Put_line("1a (base 16) is decimal" & Integer'image(To_Decimal("1a", 16)));
Put_line("1a (base 16) is decimal" & Integer'image(To_Decimal("1a", 16)));
end Number_Base_Conversion;</ada>
end Number_Base_Conversion;</ada>

=={{header|Forth}}==
Forth has a global user variable, BASE, which determines the radix used for parsing, interpretation, and printing of integers. This can handle bases from 2-36, but there are two words to switch to the most popular bases, DECIMAL and HEX.
42
2 base !
. // 101010
hex
. // 2A
decimal

Many variants of Forth support literals in some bases, such as hex, using a prefix
$ff . // 255


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

Revision as of 03:55, 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.

Ada

Ada provides built-in capability to convert between all bases from 2 through 16. This task requires conversion for bases up to 36. The following program demonstrates such a conversion using an iterative solution. <ada>with Ada.Text_Io; use Ada.Text_Io; with Ada.Strings.Fixed; With Ada.Strings.Unbounded;

procedure Number_Base_Conversion is

  Max_Base : constant := 36;
  subtype Base_Type is Integer range 2..Max_Base;
  Num_Digits : constant String := "0123456789abcdefghijklmnopqrstuvwxyz";
  Invalid_Digit : exception;
  
  function To_Decimal(Value : String; Base : Base_Type) return Integer is
     use Ada.Strings.Fixed;
     Result : Integer := 0;
     Decimal_Value : Integer;
     Radix_Offset : Natural := 0;
  begin
     for I in reverse Value'range loop
        Decimal_Value := Index(Num_Digits, Value(I..I)) - 1;
        if Decimal_Value < 0 then
           raise Invalid_Digit;
        end if; 
        Result := Result + (Base**Radix_Offset * Decimal_Value);
        Radix_Offset := Radix_Offset + 1;
     end loop;
     return Result;
  end To_Decimal;
  
  function To_Base(Value : Natural; Base : Base_Type) return String is
     use Ada.Strings.Unbounded;
     Result : Unbounded_String := Null_Unbounded_String;
     Temp : Natural := Value;
     Base_Digit : String(1..1);
  begin
     if Temp = 0 then
        return "0";
     end if;
     while Temp > 0 loop
        Base_Digit(1) := Num_Digits((Temp mod Base) + 1);
        if Result = Null_Unbounded_String then
           Append(Result, Base_Digit);
        else
           Insert(Source => Result,
              Before => 1,
              New_Item => Base_Digit);
        end if;
        Temp := Temp / Base;
     end loop;
     return To_String(Result);
  end To_Base;
  

begin

  Put_Line("26 converted to base 16 is " & To_Base(26, 16));
  Put_line("1a (base 16) is decimal" & Integer'image(To_Decimal("1a", 16)));

end Number_Base_Conversion;</ada>

Forth

Forth has a global user variable, BASE, which determines the radix used for parsing, interpretation, and printing of integers. This can handle bases from 2-36, but there are two words to switch to the most popular bases, DECIMAL and HEX.

42
2 base !
.  // 101010
hex
.   // 2A
decimal

Many variants of Forth support literals in some bases, such as hex, using a prefix

$ff .  // 255

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.

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>