convert decimal to hexadecimal using java

Categories: Java Java Programming & Coding

package r4r.in;


public class DecimalToHexconversion {

//convert decimal to hexadecimal using java

public static void main(String[] args) {

for (int i = 1; i < 160; i++) {

System.out.println("i=" + i + " -> " + toHex(i));

}

}


public static String toHex(int decimal) {

int rem;

String hex = "";

char hexchars[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };

while (decimal > 0) {

rem = decimal % 16;

hex = hexchars[rem] + hex;

decimal = decimal / 16;

}

return hex;

}


static char[] hexChar = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };


static String strHex = "";

static int num = 0;


public static String decToHex(int dec) {


if (dec != 0) {

num = dec % 16;

strHex = hexChar[num] + strHex;

dec = dec / 16;

decToHex(dec);

}

return strHex;

}


}

R4R.in Team
The content on R4R.in website is created by expert teams.
|| Blockchain || Cisco CCNA || Selenium || Internet security || Thermostat || Fashion || Online Games || Working From Home(WFH) || COVID-19 || Games || Cryptocurrency || Digital economy || Property || Sundarban National Park || NEET Course || Investment || Java || Inverter battery || PDF Drive || Online Library || Yoga || Career || Sports || SEO || Software Engineering || APPs || Digital Marketing || Certified True Copy || Mobiles || Software Testing || Books || TAX || Foreign Currency || AutoCAD || Online Course || Machine || Python || Banks || Bitcoin || Education || German Language || Festivals || Health ||