ধরা যাক আমাদের একটি অক্টাল সংখ্যা আছে। অক্টালকে বাইনারি, হেক্সাডেসিমেল ইত্যাদিতে রূপান্তর করতে, জাভা কোড নিম্নরূপ -
উদাহরণ
public class Demo{
public static String base_convert(String num, int source, int destination){
return Integer.toString(Integer.parseInt(num, source), destination);
}
public static void main(String[] args){
String my_num = "345";
int source = 8;
int destination = 2;
System.out.println("Converting the number from octal to binary: "+ base_convert (my_num, source, destination));
destination = 10;
System.out.println("Converting the number from octal to decimal : "+ base_convert (my_num, source, destination));
destination = 16;
System.out.println("Converting the number from octal to hexadecimal: "+ base_convert (my_num, source, destination));
}
} আউটপুট
Converting the number from octal to binary: 11100101 Converting the number from octal to decimal : 229 Converting the number from octal to hexadecimal: e5
ডেমো নামের একটি ক্লাসে 'base_convert' নামের একটি ফাংশন সংজ্ঞায়িত করা হয়। এই ফাংশনটি উৎস বেস থেকে গন্তব্য বেসে পূর্ণসংখ্যা পার্স করে, এটিকে একটি স্ট্রিং-এ রূপান্তর করে এবং আউটপুট হিসাবে ফেরত দেয়। মূল ফাংশনে, সংখ্যার মান, উৎস বেস এবং বিভিন্ন গন্তব্য বেসের জন্য সংজ্ঞায়িত করা হয়। 'বেস_কনভার্ট' ফাংশনটিকে প্যারামিটার হিসাবে নম্বর, উত্স এবং গন্তব্য সহ বলা হয়। প্রাসঙ্গিক আউটপুট প্রদর্শিত হয়।