Write a program to convert an octal number to a hexadecimal number.
Example 1:
Input :635
Output:19D
Example 2:
Input:540
Output:160
Approach
Java
public class OctalToHexadecimal {public static void main(String[] args) {int octal = 540;String binary = octalToBinary(octal);String hexa = binaryToHexadecimal(binary);System.out.println("Hexadecimal is " + hexa);}public static String octalToBinary(int octal) {String binary = "";while (octal > 0) {int mod = octal % 10;if (mod == 0) {binary = "000" + binary;} else {binary = decimalToBinary(mod) + binary;}octal = octal / 10;}return binary;}public static String decimalToBinary(int decimal) {String binary = "";while (decimal > 0) {// if decimal%2==0 then append 0 else append 1if (decimal % 2 == 0) {binary = "0" + binary;} else {binary = "1" + binary;}decimal = decimal / 2;}// if length is <3 the append '0' in binary prefixwhile (binary.length() < 3) {binary = '0' + binary;}return binary;}public static String binaryToHexadecimal(String binary) {String hexa = "";// check binary length divisible by 4int l = binary.length() % 4;// add remaining 0 in prefixif (l != 0) {l = 4 - l;while (l > 0) {binary = "0" + binary;l--;}}// loop run in 4-4 groupfor (int i = binary.length() - 1; i >= 0; i = i - 4) {int decimal = 0;int power = 0;for (int j = i; j > i - 4; j--) {if (binary.charAt(j) == '1') {decimal += Math.pow(2, power);}power++;}if (decimal == 0) {hexa = "0" + hexa;} else {hexa = decimalToHexadecimall(decimal) + "" + hexa;}}// convert decimal to hexadecimalreturn hexa;}public static String decimalToHexadecimall(int decimal) {String hexa = "";while (decimal > 0) {// calculate mode on base 16int mode = decimal % 16;if (mode < 10) {hexa = mode + hexa;} else {// 56+10='A', 56+11=Bhexa = (char) (56 + (mode - 1)) + "" + hexa;}decimal = decimal / 16;}return hexa;}}
C++
#include <bits/stdc++.h>using namespace std;//Function to cinvert from//decimal to binarystring decimalToBinary(int n){//varibale to store the final//resultstring res="";while(n>0){//if n%2==0 then appen 0if(n%2==0)res+='0';//if n%2==1 then appen 1elseres+='1';n=n/2;}if(res.size()<3)res+='0';//reverse the resultreverse(res.begin(),res.end());return res;}//Function to convert from octal//to binarystring octalToBinary(int octal){string binary="";while(octal>0){int mod=octal%10;if(mod==0)binary="000"+binary;elsebinary= decimalToBinary(mod)+binary;octal=octal/10;}return binary;}string binaryToHexadecimal(string s){int n=s.size();int m=n%4;if(m!=0)m=4-m;reverse(s.begin(),s.end());//make the string length factor of 4while(m--)s+='0';string ans="";reverse(s.begin(),s.end());//new length of stringn=s.size();for(int i=0;i<=n-4;i+=4){int res=0;for(int j=0;j<4;j++){if(s[i+j]=='1')res=res+pow(2,3-j);}if(res==16)ans+="10";else{m=res%16;if(res<=9)ans+=to_string(res);elseans+='A'+res%10;}}return ans;}string octalToHexadecimal(int octal){//convert octal to binarystring binary=octalToBinary(octal);//convert binary to hexadecimalstring hexadecimal=binaryToHexadecimal(binary);return hexadecimal;}int main(){int octal=635;string hexadeicmal=octalToHexadecimal(octal);cout<<"Hexadeicmal is ";cout<<hexadeicmal<<"\n";return 0;}
No comments:
Post a Comment