Statistics

Problem Statement for "ConvertBase"

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. 2934

    2

    Returns: "101101110110"

  2. 324

    16

    Returns: "144"

  3. -1231

    4

    Returns: "-103033"

  4. -15

    16

    Returns: "-F"

  5. 234

    12

    Returns: "176"

  6. 124

    11

    Returns: "103"

  7. 123

    10

    Returns: "123"

  8. 3

    2

    Returns: "11"

    This is the example above 3(base 10) = 11(base 2)

  9. 14

    10

    Returns: "14"

    The toBase is 10, so the returned value is equal to the parameter value.

  10. 18

    16

    Returns: "12"

    18(base 10) = 12(base 16) because (1 * 16 ^ 1 + 2 * 16 ^ 0 = 18).

  11. 0

    13

    Returns: "0"

    Return "0", not "-0".

  12. -5

    2

    Returns: "-101"

  13. 3

    4

    Returns: "3"

  14. 5

    3

    Returns: "12"

  15. 3423

    12

    Returns: "1B93"

  16. 2134

    12

    Returns: "129A"

  17. 532

    16

    Returns: "214"

  18. 1

    16

    Returns: "1"

  19. 12

    7

    Returns: "15"

  20. 34

    9

    Returns: "37"

  21. 15

    16

    Returns: "F"

  22. -393

    15

    Returns: "-1B3"

  23. 73

    13

    Returns: "58"

  24. 10

    15

    Returns: "A"

  25. 10

    16

    Returns: "A"

  26. 13

    16

    Returns: "D"

  27. 0

    13

    Returns: "0"

  28. 10

    14

    Returns: "A"

  29. -15

    16

    Returns: "-F"

  30. 11

    12

    Returns: "B"

  31. 14

    16

    Returns: "E"

  32. -34

    12

    Returns: "-2A"

  33. -5

    2

    Returns: "-101"

  34. -986

    2

    Returns: "-1111011010"

  35. 12

    16

    Returns: "C"

  36. 2999

    16

    Returns: "BB7"

  37. 10

    4

    Returns: "22"

  38. -135

    4

    Returns: "-2013"

  39. 224

    15

    Returns: "EE"

  40. 0

    2

    Returns: "0"

  41. 9999

    16

    Returns: "270F"

  42. -1

    5

    Returns: "-1"

  43. -5

    16

    Returns: "-5"

  44. 13

    15

    Returns: "D"

  45. 100

    10

    Returns: "100"

  46. 170

    16

    Returns: "AA"

  47. 9999

    15

    Returns: "2E69"


This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2024, TopCoder, Inc. All rights reserved.
This problem was used for: