What is the hexadecimal equivalent of number 26410 and 14010? Please give me full coding of this program.
            Asked
            
        
        
            Active
            
        
            Viewed 438 times
        
    0
            
            
        - 
                    you should do your hw youself. how will you learn otherwise? – Alec Smart Aug 16 '09 at 06:47
- 
                    [How to convert decimal to hex in JavaScript?](https://stackoverflow.com/q/57803/6521116) – LF00 Jan 19 '18 at 08:54
4 Answers
7
            
            
        You can use the toString function on numbers, and pass the base to be converted:
(264).toString(16); // 108 hex
(140).toString(16); // 8c  hex
And to do the opposite, you can use the parseInt function:
parseInt('108', 16);  // 264 dec
parseInt('8c', 16);   // 140 dec
 
    
    
        Christian C. Salvadó
        
- 807,428
- 183
- 922
- 838
2
            
            
        Since this is a Homework question, I am guessing that the questioner may be looking for an algorithm.
This is some C# code that does this without special functions:
int x = 140;
string s = string.Empty;
while (x != 0)
{
    int hexadecimal = x % 16;
    if (hexadecimal < 10)
         s = hexadecimal.ToString() + s;
    else
         s = ((char)('A' + (char)(hexadecimal - 10))).ToString() + s;
    x = (int)(x / (int)16);
}
MessageBox.Show("0X" + s);
Since this is homework, you can figure out how to translate this into JavaScript
 
    
    
        jrcs3
        
- 2,790
- 1
- 19
- 35
1
            
            
        - 
                    
- 
                    
- 
                    1Hmm, maybe you had better do it programmatically after all. `s/246/264/` – Tim Sylvester Aug 16 '09 at 06:32
1
            
            
        If you didn't know, you can use Google to convert decimal to hexadecimal or vice versa.
But it looks like @CMS already posted the Javascript code for you :) +1 to him.
 
    
    
        Mark Rushakoff
        
- 249,864
- 45
- 407
- 398
 
     
    