0

What is the hexadecimal equivalent of number 26410 and 14010? Please give me full coding of this program.

Mike
  • 43,847
  • 27
  • 105
  • 174

4 Answers4

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ó
  • 769,263
  • 179
  • 909
  • 832
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,778
  • 1
  • 19
  • 35
1

264: 0x108

140: 0x8C

No program required.

navid
  • 566
  • 6
  • 15
Noon Silk
  • 52,938
  • 6
  • 85
  • 103
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
  • 238,196
  • 44
  • 399
  • 395