TopcoderArchiveVisit Topcoder
SRM26 Nov 2002
SRM 121

ConvertBase

Problem statement, definition, constraints, and public examples.

Problem Statement

Given value, a positive or negative value in base 10, and toBase, a specified base, convert value to a number in base toBase. For example, if you are given the value 3 (in base 10) and the base 2, you must convert 3(base 10) to base 2, so you would return "11".

For the digits 10, 11, 12, 13, 14, and 15 (which may be used in bases 11 through 16), use the capital letters 'A', 'B', 'C', 'D', 'E', 'F', respectively.

For negative values, convert the absolute value of value to toBase, and then prepend a negative sign.

Definition

Class:
ConvertBase
Method:
getValue
Parameters:
int, int
Returns:
String
Method signature:
String getValue(int value, int toBase)
(be sure your method is public)

Notes

  • Your returned value should not have any leading 0's.
  • If the value is negative, the returned value should start with a "-" and then have the number in the new base.
  • There should be no spaces in the returned value.
  • If the returned value is 0, return "0" and not "-0".

Constraints

  • value will be between -10000 and 10000, inclusive.
  • base will be between 2 and 16, inclusive.

Examples

  1. 3
    2
    Returns: "11"
    This is the example above 3(base 10) = 11(base 2)
  2. 14
    10
    Returns: "14"
    The toBase is 10, so the returned value is equal to the parameter value.
  3. 18
    16
    Returns: "12"
    18(base 10) = 12(base 16) because (1 * 16 ^ 1 + 2 * 16 ^ 0 = 18).
  4. 0
    13
    Returns: "0"
    Return "0", not "-0".
  5. -5
    2
    Returns: "-101"
Back to all problems