0% found this document useful (0 votes)
30 views4 pages

Radix Converter

Radix converter

Uploaded by

Reden Tomé
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views4 pages

Radix Converter

Radix converter

Uploaded by

Reden Tomé
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

ELEBIYO TOMÉ, RUBEN. BSIT.

10/12/2024

SANTOS, OLYN DARIANE

Source Code:

import java.util.Scanner;

public class RadixConverter {

// This method takes a number and its base, converting it to all bases from 2 to 16

public static void convertToBases(String number, int originBase) {

try {

// Convert the input number from the specified base to decimal

int decimalNumber = Integer.parseInt(number, originBase);

// Display the results for conversions from base 2 to 16

System.out.println("Conversions of " + number + " from base " + originBase +


":");

for (int base = 2; base <= 16; base++) {

System.out.println("Base " + base + ": " +


convertFromDecimal(decimalNumber, base));

} catch (NumberFormatException e) {

// Handle invalid numbers for the specified base

System.out.println("The number provided is not valid for the chosen base.


Please try again.");

}
// This method converts a decimal number to the specified base

public static String convertFromDecimal(int number, int base) {

if (number == 0) return "0"; // Handle the case for zero

StringBuilder result = new StringBuilder();

// Convert the decimal number to the specified base

while (number > 0) {

int digit = number % base;

// Convert digits to hexadecimal characters if they are 10 or greater

if (digit >= 10) {

result.append((char) ('A' + (digit - 10))); // Convert to characters A-F

} else {

result.append(digit); // Append the digit as it is

number /= base; // Reduce the number for the next iteration

return result.reverse().toString(); // Reverse the result for proper order

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

while (true) {

// Prompt the user to enter the base of the number

System.out.print("Enter the origin base (2-16) or 'STOP' to end: ");

String originBaseInput = scanner.nextLine();


if (originBaseInput.equalsIgnoreCase("STOP")) {

System.out.println("Exiting the program.");

break;

// Validate the input for the origin base

int originBase;

try {

originBase = Integer.parseInt(originBaseInput);

if (originBase < 2 || originBase > 16) {

System.out.println("Invalid entry. Please provide a number between 2


and 16.");

continue;

} catch (NumberFormatException e) {

System.out.println("Invalid entry. Please provide a number between 2 and


16.");

continue;

// Ask the user for the number corresponding to the specified base

System.out.print("Enter a number in base " + originBase + ": ");

String number = scanner.nextLine().toUpperCase(); // Convert to uppercase


for consistency

// Perform the conversion and display results

convertToBases(number, originBase);

}
scanner.close(); // Close the scanner resource

You might also like