from decimal to hex code example

Example 1: convert decimal to hexadecimal in java

public class DecimalToHexExample2{    
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;  
}    
public static void main(String args[]){      
     System.out.println("Hexadecimal of 10 is: "+toHex(10));  
     System.out.println("Hexadecimal of 15 is: "+toHex(15));  
     System.out.println("Hexadecimal of 289 is: "+toHex(289));  
}}

Example 2: convert decimal to hex

// function to convert decimal to hexadecimal
function convertToHex(element,index,array) {
   return element.toString(16);
}

var decArray = new Array(23, 255, 122, 5, 16, 99);

var hexArray = decArray.map(convertToHex);
alert(hexArray); // 17,ff,a,5,10,63

Tags:

Java Example